If statement

if [ $var -eq 200 ]
then
	echo something
elif [ $var -eq 300 ]
	echo other thing
else
	echo another thing
fi
  • if syntax, if then a bracket (the bracket must have space)
  • then after the if statement
  • elif if condition is true but the other is
  • else will be evaluated if condition is not true
  • need to end it with fi
  • -eq mean equal to, equivalent to == in python
if [ ! $var -eq 200 ]
  • putting ! before the variable reverse then logic, similar to if var != 200:
    Everything in the bracket is test statement
  • in test, if the condition is met, it returns 0, else returns 1; similar to True and False in python

Integer Logic Test

  • -eq is equal to
  • -ne not equal to
  • -gt -lt greater or less than
  • -le ge greater, 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
  • -f if the file is normal
  • -d if directory exist
  • -r/w/w if a file has read, write, execute
  • -z if a variable is empty
  • -n is 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 if statements, 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 failed will print out failed, since the test [] result in exit code of 1 due 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 do and done
  • 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
done

While Loop Shorthand

while [ $(docker inspect ombi | jq .[].State.Status | tr -d '"') != "exited" ]; do echo "pls stop ombi"; sleep 0.5; done
  • separate the for, do and done and 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. *.py or python/python-app-*.py is 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
done

break - will exit the loop once it hit 3
continue - when it’s 3, it will be skipped to the next for loop