bash if then code example
Example 1: else if statement bash syntax
#!/bin/bash
read -p "Enter your marks: " marks
if [ $marks -ge 80 ]
then
echo "Very Good"
elif [ $marks -ge 50 ]
then
echo "Good"
elif [ $marks -ge 33 ]
then
echo "Just Satisfactory"
else
echo "Not OK"
fi
Example 2: bash if statement
and - &&
or - ||
if [ -r $1 ] && [ -s $1 ]
then
echo This file is useful.
fi
if [ $USER == 'bob' ] || [ $USER == 'andy' ]
then
ls -alh
else
ls
fi
Example 3: if and if bash
if [ "${STATUS}" != 200 ] && [ "${STRING}" != "${VALUE}" ]; then
Example 4: 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 5: bash else if
if [ "$animal" == "penguin" ]; then
echo "Hmmmmmm fish... Tux happy!"
elif [ "$animal" == "dolphin" ]; then
echo "Pweetpeettreetppeterdepweet!"
else
echo "*prrrrrrrt*"
fi
if TEST-COMMANDS; then
CONSEQUENT-COMMANDS;
elif MORE-TEST-COMMANDS; then
MORE-CONSEQUENT-COMMANDS;
else
ALTERNATE-CONSEQUENT-COMMANDS;
fi
Example 6: ash if statment
if [ "$USER" == "Adam" ]
then
echo "User is Adam"
else
echo "User is not Adam"
fi