Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
bash_scripting [2017/04/25 15:26] – [Conditionals] mgupton | bash_scripting [2017/07/11 19:21] (current) – mgupton | ||
---|---|---|---|
Line 1: | Line 1: | ||
======Bash Scripting====== | ======Bash Scripting====== | ||
======Conditionals====== | ======Conditionals====== | ||
- | * The special variable ''?'' | + | * The special variable '' |
<code bash> | <code bash> | ||
Line 20: | Line 20: | ||
if [$? -eq 0]; then echo " | if [$? -eq 0]; then echo " | ||
</ | </ | ||
+ | |||
+ | ======Iteration====== | ||
+ | ===for loop=== | ||
+ | <code bash> | ||
+ | for i in alpha beta gamma | ||
+ | do | ||
+ | printf " | ||
+ | done | ||
+ | |||
+ | a=alpha | ||
+ | b=beta | ||
+ | c=gamma | ||
+ | |||
+ | for j in " | ||
+ | printf " | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | for i in $( ls ); do | ||
+ | echo item: $i | ||
+ | done | ||
+ | |||
+ | LIST=" | ||
+ | for i in " | ||
+ | echo $1 | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | ===while loop=== | ||
+ | <code bash> | ||
+ | COUNTER=0 | ||
+ | |||
+ | while [ $COUNTER -lt 10 ]; do | ||
+ | echo The counter is $COUNTER | ||
+ | let COUNTER=COUNTER+1 | ||
+ | done | ||
+ | </ | ||
+ | |||
+ | ======Command Substitution/ | ||
+ | * 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.== | ||
+ | <code bash> | ||
+ | [[ -f " | ||
+ | </ | ||
+ | |||
+ | ======File I/O====== | ||
+ | |||
+ | ===Read a text file line by line.=== | ||
+ | <code bash> | ||
+ | #!/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.=== | ||
+ | <code bash> | ||
+ | while IFS=, read col1 col2 | ||
+ | do | ||
+ | echo "I got: | ||
+ | done < myfile.csv | ||
+ | </ | ||
+ | |||
+ | ======Resources====== | ||
+ | * [[http:// | ||
+ |