Scripting with BASH

variables

Command substitution

#!/bin/bash

i=`date +%s`
echo $i

Exit-codes

`$?`

Conditional structures

If-then-else

#!/bin/bash

i=1
if [ $i -eq 1 ]
then
    echo "i equals 1"
else
    echo "i does not equal 1"
fi
#!/bin/bash

i=1
j=2
if [ $i -eq 1 ]
then
    echo "i equals 1"
elif [ $j -eq 2 ]
then
    echo "j equals 2"
else
    echo "i and j do not equal 1 or 2"
fi
#!/bin/bash

i=1
[ $i -eq 1 ] && echo "i equals 1"

Case statement

#!/bin/bash

i=1

case $i in
    1) echo "One";;
    2) echo "Two";;
    3) echo "Three";;
    *) echo "Unknown";;
esac

Loops

For

#!/bin/bash

for i in {1..10}
do
    echo $i
done

While

#!/bin/bash

while :
do
    echo "Hello World!"
    sleep 1
done
#!/bin/bash

while :; do
    echo "Hello World!"
    sleep 1
done