Conditional structures

If-then-else and elif

if [ $a -eq $b ]; then
    echo "a is equal to b"
fi
if [ $a -eq $b ]; then
    echo "a is equal to b"
else
    echo "a is not equal to b"
fi
if [ $a -eq $b ]; then
    echo "a is equal to b"
elif [ $a -eq $c ]; then
    echo "a is not equal to b"
else
    echo "a is not equal to b or c"
fi

Test Operators

if [ -f filename ]; then
    echo "file exists"
fi
if [ -d dirname ]; then
    echo "directory exists"
fi
if [ ! -d dirname ]; then
    mkdir dirname
fi

Case

case $a in
    1) echo "a is 1" ;;
    2) echo "a is 2" ;;
    3) echo "a is 3" ;;
    *) echo "a is not 1, 2 or 3" ;;
esac

Select