- if-else
- Evaluate
$?
withif
statement, usingif [[ ]]
andif (( ))
- Evaluate using
if [ ]
- Using
if (( ))
and elif statement - Comparing strings inside
if
statement - Using only
[ ]
- Using
if
andtest
(then you can omit[ ]
) - Use a command as sentence of
if
- Use double square bracket
[[ ]]
to test strings - Use
test
inside andif
statement - Equivalence using
if-else
statement andcommand && command || command
- Multiple conditions on if statement
1. if-else
if echo "hello" && echo "World"; then echo "OK"; fi
hello
World
OK
❯ echo "Hello World"
Hello World
❯ if [[ $? ]]; then echo "successful execution $?"; else echo "execution failed"; fi
successful execution 0
❯ echo "Hello World"
Hello World
❯ if (( $? )); then echo "execution failed $?"; else echo "successfull execution, but \$? = $?"; fi
successfull execution, but $? = 1
if [ $# -lt 3 ]; then
printf "%b" "Error. Not enough arguments.\n"
printf "%b" "usage: myscript file1 op file2\n"
# exit 1 # uncomment on a script
fi
Alternatively
if (( $# < 3 )); then
printf "%b" "Error. Not enough arguments.\n"
printf "%b" "usage: myscript file1 op file2\n"
# exit 1 # uncomment on a script
fi
if (( $# < 3 )); then
printf "%b" "Error. Not enough arguments.\n"
printf "%b" "usage: myscript file1 op file2\n"
# exit 1 # uncomment on a script
elif (( $# > 3 )); then
printf "%b" "Error. Too many arguments.\n"
printf "%b" "usage: myscript file1 op file2\n"
# exit 2 # uncomment on a script
else
printf "%b" "Argument count correct. Proceeding...\n"
fi
choice="yes"
if [ "$choice" = "yes" ]; then
echo "Your choice is yes"
fi
Output:
Your choice is yes
result=1
[ $result = 1 ] && { echo "Result is 1" }
Result is 1
if test $# -lt 3; then
echo try again.
fi
Output
try again
mkdir directory
if [ $? = 0 ]; then
echo "directory was created successfully"
else
echo "directory was already created"
fi
move the command into the if
statement
if mkdir directory; then
echo "directory was created successfully"
else
echo "directory was already created"
fi
if [[ "${var}" == "some_string" ]]; then
echo "do something"
fi
# -z (string length is zero) and -n (string length is not zero) are
# preferred over testing for an empty string
if [[ -z "${my_var}" ]]; then
echo "do something"
fi
# This is OK (ensure quotes on the empty side), but not preferred:
if [[ "${my_var}" == "" ]]; then
echo "do something"
fi
touch ~/file.txt
if test -f ~/file.txt; then
echo "file exist"
else
echo "file does not exist"
fi
console output
file exist"
if cp file.txt $HOME/iOS-Projects; then
echo "Done"
else
echo "failed"
exit 1
fi
Equivalence:
cp file.txt $HOME/iOS-Projects && { echo "Done" } || { echo "Failed"; exit 1 }
In this case we should use the exit command in order to point out that the command was not successfully executed"
#!/bin/bash
var_one=1
# Only one var might be set or none of them
var_two=
var_three=1
# Only one flag might be set or none of them
flag_one=
flag_two=
flag_three=
# draw table of thruth
if [[ "${var_one}" ]] \
&& { [[ "${var_two}" ]] || [[ "${var_three}" ]]; } \
&& { [[ -z "${flag_one}" ]] && [[ -z "${flag_two}" ]] && [[ -z "${flag_three}" ]]; }; then
echo "flag_one=$flag_one"
echo "flag_two=$flag_two"
echo "flag_three=$flag_three"
echo "all flags are length zero"
exit 1
fi
./if-statement.sh
Console output:
flag_one=
flag_two=
flag_three=
all flags are length zero