bash shell if statement code example
Example 1: bash if statement
and - &&
or - ||
# and example
if [ -r $1 ] && [ -s $1 ]
then
echo This file is useful.
fi
# or example
if [ $USER == 'bob' ] || [ $USER == 'andy' ]
then
ls -alh
else
ls
fi
Example 2: bash if
#!/bin/bash
foo="qux"
bar="qux"
if [ "$foo" = "$bar" ]; then
echo "The strings are equal."
else
echo "The strings aren't equal."
fi
Example 3: codition in bash
# short if codition
[ $(( 2 + 2 )) -eq '4' ] && echo "yes"
# long if codition
if [ $(( 2 + 2 )) -eq '4' ]
then
echo "yess"
fi