From 4b0a965e4ae900eff490dd8a6ece6e81d3af1ff1 Mon Sep 17 00:00:00 2001 From: Marina Wahl Date: Tue, 1 Jul 2014 19:51:31 -0400 Subject: [PATCH] more bash examples --- shell_scripts/lessons/TRICKS.md | 53 ++++++++++++++ shell_scripts/lessons/arguments.ssh | 15 ++++ shell_scripts/lessons/cheesboard.sh | 18 +++++ shell_scripts/lessons/echo_cmds.sh | 9 +++ .../for_loop_example.sh} | 0 shell_scripts/lessons/if_example.sh | 70 +++++++++++++++++++ shell_scripts/lessons/inputs.sh | 7 ++ shell_scripts/lessons/linux_console.sh | 15 ++++ shell_scripts/lessons/shell_variables.sh | 12 ++++ 9 files changed, 199 insertions(+) create mode 100644 shell_scripts/lessons/TRICKS.md create mode 100755 shell_scripts/lessons/arguments.ssh create mode 100755 shell_scripts/lessons/cheesboard.sh create mode 100755 shell_scripts/lessons/echo_cmds.sh rename shell_scripts/{neat_examples/loop_example.sh => lessons/for_loop_example.sh} (100%) create mode 100755 shell_scripts/lessons/if_example.sh create mode 100755 shell_scripts/lessons/inputs.sh create mode 100755 shell_scripts/lessons/linux_console.sh create mode 100755 shell_scripts/lessons/shell_variables.sh diff --git a/shell_scripts/lessons/TRICKS.md b/shell_scripts/lessons/TRICKS.md new file mode 100644 index 0000000..b18c8ab --- /dev/null +++ b/shell_scripts/lessons/TRICKS.md @@ -0,0 +1,53 @@ +Wild Cards +---------- + +Use *, ? (for one char), and [abc] for a series of options. Example: + +$ ls [abc]* + + +More commands on one line +------------------------- + +Use ; +$ date;who + + +Redirection of Standard output/input +----------------------------------- + +>, >>, < + +Examples: + +$ sort < filename > sorted_names + + +Pipes +----- +A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. + + +Processes and PID +----------------- +An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process. + +ps - show current running +kill {PID} - kill process +kill 0 - stop all processes except shell +& - background process +ps aux - display the owner of the processes along with the processes +ps -ag - get information of all running processes +ps ax | grep processes-you-want-to-see +top - see currently running processes and other information like memory and CPU +pstree - display a tree of processes + + + +Useful Cmds: +------------ + +List size of folders +$ du -sh * + + diff --git a/shell_scripts/lessons/arguments.ssh b/shell_scripts/lessons/arguments.ssh new file mode 100755 index 0000000..87f51e2 --- /dev/null +++ b/shell_scripts/lessons/arguments.ssh @@ -0,0 +1,15 @@ +#! /bin/bash +# script to test for loop and arguments + +if [ $# -eq 0 ] + then + echo "Error - Number missing form command line argument" + echo "Syntax : $0 number" + echo "Use to print multiplication table for given number" + exit 1 +fi +n=$1 +for i in 1 2 3 4 5 6 7 8 9 10 +do + echo "$n * $i = `expr $i \* $n`" +done \ No newline at end of file diff --git a/shell_scripts/lessons/cheesboard.sh b/shell_scripts/lessons/cheesboard.sh new file mode 100755 index 0000000..3674c92 --- /dev/null +++ b/shell_scripts/lessons/cheesboard.sh @@ -0,0 +1,18 @@ +#! /bin/bash +# script to print a chessboard + +for (( i = 1; i <= 9; i++ )) +do + for (( j = 1; j <= 9; j++)) + do + tot=`expr $i + $j` + tmp=`expr $tot % 2` + if [ $tmp -eq 0 ]; then + echo -e -n "\033[47m " + else + echo -e -n "\033[40m " + fi + done + echo -e -n "\033[40m" # set to black + echo"" # print new line +done \ No newline at end of file diff --git a/shell_scripts/lessons/echo_cmds.sh b/shell_scripts/lessons/echo_cmds.sh new file mode 100755 index 0000000..aa9f09b --- /dev/null +++ b/shell_scripts/lessons/echo_cmds.sh @@ -0,0 +1,9 @@ +#! /bin/bash +echo "The echo commands are (use echo -e):" +echo +echo -e "\a alert " +echo -e "\n new line" +echo -e "\t horizontal tab" +echo -e "\c suppress trailing line" +echo -e "\r carriage return" +echo -e "\b backspace" \ No newline at end of file diff --git a/shell_scripts/neat_examples/loop_example.sh b/shell_scripts/lessons/for_loop_example.sh similarity index 100% rename from shell_scripts/neat_examples/loop_example.sh rename to shell_scripts/lessons/for_loop_example.sh diff --git a/shell_scripts/lessons/if_example.sh b/shell_scripts/lessons/if_example.sh new file mode 100755 index 0000000..f95dd02 --- /dev/null +++ b/shell_scripts/lessons/if_example.sh @@ -0,0 +1,70 @@ +#! /bin/bash +# +#Syntax: +# if condition +# then +# if condition +# then +# ..... +# .. +# do this +# else +# .... +# .. +# do this +# fi +# else +# ... +# ..... +# do this +# fi +# +# You can also use +# elif +# ... +# then +# +# + + +echo "Here an example of how to use if condition for numbers:" +echo +echo "Type a number from 1 to 10:" +read number +if test $number -gt 5 +then +echo "This number is larger than 5" +fi +if test $number -lt 5 +then +echo "This number is smaller than 5" +fi +echo +echo "Operators are: -eq, -ne, -lt, -gt, -ge" +echo +echo "****************************************************" +echo +echo "Here an example of how to use if condition for strings:" +echo +echo "Type a number your name:" +read name +if test $name != "john" +then +echo "Your name is not John, its $name" +else +echo "Your name is john! What the odds?" +fi +echo +echo "Operators are: =, !=, -n (not null, exist), -z null (exist)" +echo +echo "****************************************************" +echo +echo "Here an example of how to use if condition for files:" +echo +echo "Operators are: -s (non empty), -f (exist), -d (directory exist), -w (writeable), -r (read-only), -x (executable)" +echo +echo "****************************************************" +echo +echo "Here an example of how to use logical operators:" +echo +echo "Operators are: ! (NOT), -a (AND), -o (OR)" \ No newline at end of file diff --git a/shell_scripts/lessons/inputs.sh b/shell_scripts/lessons/inputs.sh new file mode 100755 index 0000000..90c0c96 --- /dev/null +++ b/shell_scripts/lessons/inputs.sh @@ -0,0 +1,7 @@ +#! /bin/bash +echo "We use read to read an input" +echo +echo "Type your name: " +read fname +echo "You name is $fname" +echo \ No newline at end of file diff --git a/shell_scripts/lessons/linux_console.sh b/shell_scripts/lessons/linux_console.sh new file mode 100755 index 0000000..c236388 --- /dev/null +++ b/shell_scripts/lessons/linux_console.sh @@ -0,0 +1,15 @@ +#! /bin/bash +echo "The Linux Console variables are:" +echo +echo "\33 is the escape char, to take an action. Ex: using \033[34m:" +echo -e "\033[34m Hello! \033[0m" +echo +echo " [ is the start, 34 the parameter, m is the letter (action)" +echo " letters can be h, l, m, q, s, u..." +echo " parameters can be 0, 1 (bold), 2 (dim intensity), 5 (blink), 7 (reverse), 11, 25, 27, 30-27 (colors), 4047 (bkgd colors)..." +echo +echo "For example:" +echo -e "\033[5m Hey! \033[0m" +echo +echo "Now, quotes... the black quote is used to execute a cmd (\`cmd\`)" +echo \ No newline at end of file diff --git a/shell_scripts/lessons/shell_variables.sh b/shell_scripts/lessons/shell_variables.sh new file mode 100755 index 0000000..6bc771e --- /dev/null +++ b/shell_scripts/lessons/shell_variables.sh @@ -0,0 +1,12 @@ +#! /bin/bash +echo "The variable of the system are:" +echo +echo "BASH_VERSION: " $BASH_VERSION +echo "LOGNAME: " $LOGNAME +echo "OSTYPE: " $OSTYPE +echo "PATH: " $PATH +echo "PWD: " $PWD +echo "USERNAME: " $USERNAME +echo +echo "Linux distribution, name, arch: $ uname -a" +echo \ No newline at end of file