bash_scripting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
bash_scripting [2017/04/25 16:08] – [Iteration] mguptonbash_scripting [2017/07/11 19:21] (current) mgupton
Line 1: Line 1:
 ======Bash Scripting====== ======Bash Scripting======
 ======Conditionals====== ======Conditionals======
-  * The special variable ''?'' has the exit/return value of the last program/command that ran.+  * The special variable ''$?'' has the exit/return value of the last program/command that ran.
  
 <code bash> <code bash>
Line 58: Line 58:
 done done
 </code> </code>
 +
 +======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.
 +<code>
 +$(<command expression>)
 +</code>
 +<code>
 +text=$(cat alpha.txt)
 +</code>
 +<code>
 +`<command expression>`
 +</code>
 +
 +======File Manipulation======
 +==Delete a file if it exists.==
 +<code bash>
 +[[ -f "$file" ]] && rm -f "$file"
 +</code>
 +
 +======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
 +</code>
 +
 +===Read each line of a file as a CSV record.===
 +<code bash>
 +while IFS=, read col1 col2
 +do
 +    echo "I got:$col1|$col2"
 +done < myfile.csv
 +</code>
 +
 +======Resources======
 +  * [[http://wiki.bash-hackers.org/|The Bash Hackers Wiki]]
 +
  • bash_scripting.1493136530.txt.gz
  • Last modified: 2017/04/25 16:08
  • by mgupton