=====Write Simple Bash Scripts=====
in vim enable line numbering with
esc
:set nu
using **awk**
awk is used to get bits of files for example
awk -F: `{print $1}`
prints out the first entry (from the $1) of a file that uses : as a separator
====Loops====
===for===
The structure is
for VAR in list of space separated items; do action $VAR; done
Examples
For HOST in server a server; do echo $HOST; done
For HOST in server{a,b}; do echo $HOST; done
For HOST in server{a..b}; do echo $HOST; done
These all do the same thing
===if===
if statements use following structure
if ; then
...
else
...
fi
Conditions are from command **test**
[user@host ~]$ [[ 1 -eq 1 ]]; echo $?
0
[user@host ~]$ [[ 1 -ne 1 ]]; echo $?
1
[user@host ~]$ [[ 8 -gt 2 ]]; echo $?
0
[user@host ~]$ [[ 2 -ge 2 ]]; echo $?
0
[user@host ~]$ [[ 2 -lt 2 ]]; echo $?
1
[user@host ~]$ [[ 1 -lt 2 ]]; echo $?
0
The following examples demonstrate the Bash string comparison operators:
[user@host ~]$ [[ abc = abc ]]; echo $?
0
[user@host ~]$ [[ abc == def ]]; echo $?
1
[user@host ~]$ [[ abc != def ]]; echo $?
0
===while===
will run a a command while a condition is meet.
here is an interesting example
while ! test -f test_file; do sleep 1s; done
this checks to see if a file called test_file exists if it does the while loop exits if not it keeps running
===case===
Case is a comparison of input with know conditions
here is an example
read -p "give me an animal:" animal
case $animal in
person)
echo -e " aperson has 2 legs \n"
;;
cat)
echo -e "a cat has 4 lets \n"
;;
*)
ech0 -e "this covers all other conditions \n"
exit 1
;;
esac
====Regex====
[[:linux:classnotes:RH134:bash:regex | Regular Expressions ]]