Conditional Statements IF/ELIF/ELSE
IF / ELSE
if [ condition ]
then
# do something
else
# do something else
fi
Be very particular of the whitespace around [
and ]
. It is [ condition ]
and not [condition]
OR
if [ condition ]; then
# do something
else
# do something else
fi
where you can place then
at the end of the same line as your if
condition, separated by a semi-colon ;
.
For example:
# Take user input
read input
# See if user provided twitter
if [ "$input" == "twitter.com" ]; then
# show error
echo "can not ping this!"
else
# ping three times
ping -c3 ${input}
fi
ELIF
ELIF is ELse IF.
# Take user input
read input
# See if user provided twitter
if [ $input == "twitter.com" ]; then
# show error
echo "can not ping this!"
elif [ $input == "google.com" ]; then
echo "Why always Google? -_-"
else
# ping three times
ping -c3 ${input}
fi