xxxxxxxxxx
a=2
b=2
if [ $a -lt $b ]
then
echo "a is less than b"
elif [ $a == $b ]
then
echo "a is equal to b"
else # Here a is not <= b, so a > b
echo "a is greater than b"
fi
xxxxxxxxxx
if [ "$result" == "1" ]
then
echo "test" >> /home/samurai/test.txt
fi
if [[ "$result" -eq "1" ]]
then
echo "test" >> /home/samurai/test.txt
fi
xxxxxxxxxx
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
xxxxxxxxxx
# Basic syntax
if [[ condition_1 ]]; then
echo "Code to execute if condition_1 is true"
elif [[ condition_2 ]]; then
echo "Code to execute if condition_1 is false and condition_2 is true"
else
echo "Code to execute if condition_1 and condition_2 are false"
fi
# Note, the syntax for the one-line equivalent is:
if [[ condition_1 ]]; then echo "Code to execute if condition_1 is true"; elif [[ condition_2 ]]; then echo "Code to execute if condition_1 is false and condition_2 is true"; else echo "Code to execute if condition_1 and condition_2 are false"; fi
# Note to self, see this link for more on bash operators and [ ] vs [[ ]]:
# https://tldp.org/LDP/abs/html/comparison-ops.html
xxxxxxxxxx
if [[ some condition ]]; then
do_this
elif [[ another condition ]]; then
do_that_a
elif [[ yet another condition]]; then
do_that_b
else
do_that_default_thing
fi
xxxxxxxxxx
#!/bin/sh
x=10
y=5
if [ $x -gt $y ]; then
echo "$x is greater than $y"
elif [ $x -lt $y ]; then
echo "$x is less than $y"
else
echo "$x is equal to $y"
fi
##################
if [[ CONDITION-TO-TEST ]]
then
CODE-TO-EXECUTE-1
elif [[ NEXT-CONDITION-TO-TEST ]]
then
CODE-TO-EXECUTE-2
elif [[ NEXT-CONDITION-TO-TEST ]]
then
CODE-TO-EXECUTE-2
else
CODE-TO-EXECUTE-2
fi
eg :
xxxxxxxxxx
# Very basic syntax
if [[ condition_1 ]]; then
echo "Code to execute if condition_1 is true"
elif [[ condition_2 ]]; then
echo "Code to execute if condition_1 is false and condition_2 is true"
else
echo "Code to execute if condition_1 and condition_2 are false"
fi
# Note, the syntax for the one-line equivalent is:
if [[ condition_1 ]]; then echo "Code to execute if condition_1 is true"; elif [[ condition_2 ]]; then echo "Code to execute if condition_1 is false and condition_2 is true"; else echo "Code to execute if condition_1 and condition_2 are false"; fi
# Note to self, see this link for more on bash operators and [ ] vs [[ ]]:
# https://tldp.org/LDP/abs/html/comparison-ops.html
xxxxxxxxxx
if [[ condition ]]
then
<execute command>
else
<execute another command>
fi