If statement
if [ $var -eq 200 ]
then
echo something
elif [ $var -eq 300 ]
echo other thing
else
echo another thing
fiifsyntax, if then a bracket (the bracket must have space)thenafter the if statementelifif condition is true but the other iselsewill be evaluated if condition is not true- need to end it with
fi -eqmean equal to, equivalent to==in python
if [ ! $var -eq 200 ]- putting
!before the variable reverse then logic, similar toif var != 200:
Everything in the bracket is test statement - in test, if the condition is met, it returns 0, else returns 1; similar to
TrueandFalsein python
Integer Logic Test
-eqis equal to-nenot equal to-gt-ltgreater or less than-legegreater, less or equal
=and!=also works for integers, but need to have space between 2 integers[ 10 = 10]
String/File Logic Test
https://kapeli.com/cheat_sheets/Bash_Test_Operators.docset/Contents/Resources/Documents/index
=or!=is equal or not-fif the file is normal-dif directory exist-r/w/wif a file has read, write, execute-zif a variable is empty-nis a variable is nonempty
Command Chaining
; semi-colon, all command after will run regardless of exit status
&& double ampersand, all commands after will only run if previous command is successful
- in the context of
ifstatements, both have to be true for it to run
||OR operator, command after it will run only if the command before it failed - this is why
[ 1 = 2 ] || echo failedwill print outfailed, since the test[]result in exit code of1due to failed condition, and echo failed will only run when the previous command exited with nonzero status - similarly
if [ 1 != 1 ] || [ 1 = 1 ]will also run, since in the OR statement means if any of the tests are true, it will do the if loop, even though the first condition is false
&single ampersand, the command will run at the same time in the background
While Loops
while [ $var -le 10 ]
do
echo $var
done- same syntax as if statement
- put the commands in
doanddone - the commands will run as long as while condition is true
Example command that keep on trying a command until it succeed
while [ $? -ne 0 ]
do
failed command
doneWhile Loop Shorthand
while [ $(docker inspect ombi | jq .[].State.Status | tr -d '"') != "exited" ]; do echo "pls stop ombi"; sleep 0.5; done- separate the
for,doanddoneand additional commands with;
For Loops
for i in 1 2 3 4 5 "int"
do
echo $i
done- same concept and syntax as while loop
- will iterate over a list (
{1..5}) or (1 2 3 4 5)
There are additional bash functions to create a list - eg.
*.pyorpython/python-app-*.pyis a list containing all the python files in a directory,*can be used to wildcard match
Bash Shorthand for For Loops
for i in *.txt*; do echo $i; done- same syntax as while loop shorthand
Breaking Out of a Loop
The mechanism for breaking/continue is the same as python
for i in {1..5}
do
if [ $i == 3 ]; then
break
fi
echo $i # break = 1 2; continue = 1 2 4 5
donebreak - will exit the loop once it hit 3
continue - when it’s 3, it will be skipped to the next for loop