More information: http://linuxconfig.org/Bash_scripting_Tutorial ---------------------------------------------------------------- --Basic Variable-- #!/bin/bash STRING="Hello World" echo $STRING --Read in Variable-- #!/bin/bash echo -e "Input: \c " read word echo "Output: $word" echo -e "Enter two words? " read word1 word2 echo "Output: \"$word1\" \"$word2\"" echo -e "How do you feel about bash scripting? " # read command now stores a reply into the default build-in variable $REPLY read echo "You said $REPLY" echo -e "What are your favorite colours ? " # -a makes read command to read into an array read -a colours echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)" -- Variable from a command-- var=$(command-name-here) var=$(command-name-here arg1) var=$(/path/to/command) var=$(/path/to/command arg1 arg2) --Command in Echo-- echo "Text $(command-name)" echo "Here are the users $(w)" --Arrays-- #!/bin/bash #Declare array with 4 elements ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux ) # get number of elements in the array ELEMENTS=${#ARRAY[@]} # echo each element in array # for loop for (( i=0;i<$ELEMENTS;i++)); do echo ${ARRAY[${i}]} done --Read in a File-- #!/bin/bash #Declare array declare -a ARRAY # Link filedescriptor 10 with stdin exec 10<&0 # stdin replaced with a file supplied as a first argument exec < $1 let count=0 while read LINE; do ARRAY[$count]=$LINE ((count++)) done echo Number of elements: ${#ARRAY[@]} # echo array's content echo ${ARRAY[@]} # restore stdin from filedescriptor 10 # and close filedescriptor 10 exec 0<&10 10<&- --IF / Else-- #!/bin/bash directory="./BashScripting" # bash check if directory exists if [ -d $directory ]; then echo "Directory exists" else echo "Directory does not exists" fi --Arithmentic Comparison-- -lt -gt -le -ge -eq -ne < > <= >= == #!/bin/bash # declare integers NUM1=2 NUM2=2 if [ $NUM1 -eq $NUM2 ]; then echo "Both Values are equal" else echo "Values are NOT equal" fi --String Comparison-- = != < > -n s1 -z s1 Equal Not equal Less then Greater then String s1 is not empty String s1 is empty #!/bin/bash #Declare string S1 S1="Bash" #Declare string S2 S2="Scripting" if [ $S1 = $S2 ]; then echo "Both Strings are equal" else echo "Strings are NOT equal" fi --FOR Loop-- #!/bin/bash # bash for loop for f in $( ls /var/ ); do echo $f done Running for loop from bash shell command line: $ for f in $( ls /var/ ); do echo $f; done -----OTHER WAYS to USE FOR LOOP----- for VARIABLE in 1 2 3 4 5 .. N do command1 command2 commandN done ------------------------------------ for VARIABLE in file1 file2 file3 do command1 on $VARIABLE command2 commandN done ------------------------------------ for OUTPUT in $(Linux-Or-Unix-Command-Here) do command1 on $OUTPUT command2 on $OUTPUT commandN done ------------------------------------ for i in 1 2 3 4 5 do echo "Welcome $i times" done ------------------------------------ for i in {1..5} do echo "Welcome $i times" done ------------------------------------ for i in {0..10..2} do echo "Welcome $i times" done for (( EXP1; EXP2; EXP3 )) do command1 command2 command3 done ------------------------------------ for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done ------------------------------------ Infinite for loop can be created with empty expressions, such as: #!/bin/bash for (( ; ; )) do echo "infinite loops [ hit CTRL+C to stop]" done ------------------------------------ You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop: for I in 1 2 3 4 5 do statements1 #Executed for all values of ''I'', up to a disaster-condition if any. statements2 if (disaster-condition) then break #Abandon the loop. fi statements3 #While good and, no disaster-condition. done ------------------------------------ #!/bin/bash for file in /etc/* do if [ "${file}" == "/etc/resolv.conf" ] then countNameservers=$(grep -c nameserver /etc/resolv.conf) echo "Total ${countNameservers} nameservers defined in ${file}" break fi done ------------------------------------ for I in 1 2 3 4 5 do statements1 #Executed for all values of ''I'', up to a disaster-condition if any. statements2 if (condition) then continue #Go to next iteration of I in the loop and skip statements3 fi statements3 done --While Loop-- #!/bin/bash COUNT=6 # bash while loop while [ $COUNT -gt 0 ]; do echo Value of count is: $COUNT let COUNT=COUNT-1 done --Until Loop-- #!/bin/bash COUNT=0 # bash until loop until [ $COUNT -gt 5 ]; do echo Value of count is: $COUNT let COUNT=COUNT+1 done --Functions -- !/bin/bash # BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER function function_B { echo Function B. } function function_A { echo $1 } function function_D { echo Function D. } function function_C { echo $1 } # FUNCTION CALLS # Pass parameter to function A function_A "Function A." function_B # Pass parameter to function C function_C "Function C." function_D --Select-- #!/bin/bash PS3='Choose one word: ' # bash select select word in "linux" "bash" "scripting" "tutorial" do echo "The word you have selected is: $word" # Break, otherwise endless loop break done exit 0 --CASE Condition-- #!/bin/bash echo "What is your preferred programming / scripting language" echo "1) bash" echo "2) perl" echo "3) phyton" echo "4) c++" echo "5) I do not know !" read case; #simple case bash structure # note in this case $case is variable and does not have to # be named case this is just an example case $case in echo "You selected bash";; echo "You selected perl";; echo "You selected phyton";; echo "You selected c++";; exit esac