======Bash Scripting====== ======Conditionals====== * The special variable ''$?'' has the exit/return value of the last program/command that ran. if [ $? -eq 0 ]; then echo "All is well." exit 0 elif [ $? -eq 1 ]; then echo "Doh!" exit 1 else echo "Really?!" exit 2 fi ===One line if statement=== if [$? -eq 0]; then echo "OK"; else echo "Error"; fi ======Iteration====== ===for loop=== for i in alpha beta gamma do printf "$i\n" done a=alpha b=beta c=gamma for j in "$a" "$b" "$c"; do printf "$j\n" done for i in $( ls ); do echo item: $i done LIST="$(ls *.html)" for i in "$LIST"; do echo $1 done ===while loop=== COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done ======Command Substitution/Interpolation====== * Command substitution will run the specified command and replace the command with the output from the command in the location where it is specified as part of input to some other command. $() text=$(cat alpha.txt) `` ======File Manipulation====== ==Delete a file if it exists.== [[ -f "$file" ]] && rm -f "$file" ======File I/O====== ===Read a text file line by line.=== #!/bin/bash while read line do name=$line echo "Text read from file - $name" done < $1 ===Read each line of a file as a CSV record.=== while IFS=, read col1 col2 do echo "I got:$col1|$col2" done < myfile.csv ======Resources====== * [[http://wiki.bash-hackers.org/|The Bash Hackers Wiki]]