Bourne Again SHell
GNU Bash
- Bash is GNU's project
- sh-compatible
- incorporates Korn shell and C shell features
- IEEE POSIX SHELL AND TOOLS OF IEEE POSIX STANDARDS 10003.1
what is bash
Bash is the shell or Command Language Interpreter, for GNU operating system
-
the name is a pun on Stephen Bourne
-
author of the direct ancestor of the current unix shell
- appeared in 7th ed. of Bell Labs Research version of Unix
-
author of the direct ancestor of the current unix shell
what is shell
- a macro processor that executes commands macro processor: functionality where text and symbols are expanded to create larger expressions
Bash Scripts
Bash Commands
chmod
bc
arbitrary-precision arithmetic language
cut
paste
cat
groups
print group memberships for each USERNAME or current process
grep
print lines that match patterns
wc
wordcount
history
Many programs read from the user a line at a time
- GNU history library keeps track of those lines
e.g. `history | grep jlb | grep 592`
sed
`sed `` sn.csv` same as `cat sn.csv`
deleting lines (with file unchanged)
`sed 'd' sn.csv`
delete first line `sed '1d' sn.csv`
regex will match a string that contains either no characters or only white space characters `sed '/^
\s*$/d' factorial.py`
regex `^#` matches comment lines, so we connect `^\s*
$` with an 'or' to `^#`, we insert a `|` `sed '/^
\s*$\|^#/d' factorial.py`
print only the commented lines? `sed '/^#/p' factorial.py`
create a javascript file from lat lon coordinates `sed `s/^/new google.maps.LatLng(' latlon > latlon2` `sed 's/$)/' latlon2 > latlon3`
-
in-place flag `-i`
-
fixing errors in multiple files
`grep -i [Rr]acoon *` `sed -i 's/racoon/raccoon/g' 1.[35]*` `sed -i 's/Racoon/Raccoon/g' 1.[35]*`
awk
-
awk scripts consist of one or more `pattern { action }` blocks
predefined variables
- RS record separator is a newline character
- FS/OFS field separator/output field separator
- NR currrent record number
- NF the number of fields (columns) in the currrent record
skip first line `awk 'NR>1' sn.csv`
print lines 2 thru 4 `awk 'NR>1 && NR<5' sn.csv`
selecting fields `awk '{print $1,$3}' FS=, sn.csv`
BEGIN and END `awk 'BEGIN {FS=OFS=","} {print $1,$3}' sn.csv`
arithmetic column sum `awk 'NR>1 {s+=$3} END {print s}' FS=, sn.csv` cum sum `awk 'NR>1 {s+=$3; print s}' FS=, sn.csv`
average `awk 'NR>1 {s+=$3; c+=1} END {print s/c}' FS=, sn.csv`