The previous section introduced the concept that there are several reserved words and a number of characters that have an effect on the operation of Bash. The most basic, and probably most widely used conditional logic is with if
and else
statements. Let's use an example code snippet:
#!/bin/bash AGE=17 if [ ${AGE} -lt 18 ]; then echo "You must be 18 or older to see this movie" fi
Note
Notice the space after or before the square brackets in the if
statement. Bash is particularly picky about the syntax of bracketing.
If we are evaluating the variableage
using less than (<
) or -lt
(Bash offers a number of syntactical constructs for evaluating variables), we need to use an if
statement. In our if
statement, if $AGE
is less than 18
, we echo the message You must be 18 or older to see this movie
. Otherwise, the script will not execute the echo
statement and will continue execution. Notice that the if
statement ends with the reserved word fi
. This is not a mistake and is required by Bash syntax.
Let's say we want to add a catchall using else
. If the then
command block of the if
statement is not satisfied, then the else
will be executed:
#!/bin/bash AGE=40 if [ ${AGE} -lt 18 ] then echo "You must be 18 or older to see this movie" else echo "You may see the movie!" exit 1 fi
With AGE
set to the integer value 40
, the then
command block inside the if
statement will not be satisfied and the else
command block will be executed.
Let's say we want to introduce another if
condition and use elif
(short for else if):
#!/bin/bash AGE=21 if [ ${AGE} -lt 18 ]; then echo "You must be 18 or older to see this movie" elif [ ${AGE} -eq 21 ]; then echo "You may see the movie and get popcorn" else echo "You may see the movie!" exit 1 fi echo "This line might not get executed"
If AGE
is set and equals 21
, then the snippet will echo
:
You may see the movie and get popcorn This line might not get executed
Using if
, elif
, and else
, combined with other evaluations, we can execute specific branches of logic and functions or even exit our script. To evaluate raw binary variables, use the following operators:
-gt
(greater than >)-ge
(greater or equal to >=)-lt
(less than <)-le
(less than or equal to <=)-eq
(equal to)-nq
(not equal to)
As mentioned in the variables subsection, numeric values are different from strings. Strings are typically evaluated like this:
#!/bin/bash MY_NAME="John" NAME_1="Bob" NAME_2="Jane" NAME_3="Sue" Name_4="Kate" if [ "${MY_NAME}" == "Ron" ]; then echo "Ron is home from vacation" elif [ "${MY_NAME}" != ${NAME_1}" && "${MY_NAME}" != ${NAME_2}" && "${MY_NAME}" == "John" ]; then echo "John is home after some unnecessary AND logic" elif [ "${MY_NAME}" == ${NAME_3}" || "${MY_NAME}" == ${NAME_4}" ]; then echo "Looks like one of the ladies are home" else echo "Who is this stranger?" fi
In the preceding snippet, you might notice that the MY_NAME
variable will be executed and the string John is home after some unnecessary AND logic
will be echoed to the console. In the snippet, the logic flows like this:
- If
MY_NAME
is equal toRon
, thenecho "Ron is home from vacation"
- Else if
MY_NAME
is not equal toNAME_1
ANDMY_NAME
is not equal toNAME_2
ANDMY_NAME
is equal toJohn
, thenecho "John is home after some unnecessary AND logic"
- Else if
MY_NAME
is equal toNAME_3
ORMY_NAME
is equal toNAME_4
, thenecho "Looks like one of the ladies"
- Else
echo "Who is this stranger?"
Notice the operators: &&
, ||
, ==
, and !=
&&
(means and)||
(means or)==
(is equal to)!=
(not equal to)-n
(is not null or is not set)-z
(is null and zero length)
Note
Null means not set or empty in the world of computing. There are many different types of operators or tests that can be used in your scripts. For more information, check out: http://tldp.org/LDP/abs/html/comparison-ops.html and https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html#Shell-Arithmetic
If a single level of if
statements is not enough and you would like to have additional logic within an if
statement, you can create nested conditional statements. This can be done in the following way:
#!/bin/bash USER_AGE=18 AGE_LIMIT=18 NAME="Bob" # Change to your username if you want to execute the nested logic HAS_NIGHTMARES="true" if [ "${USER}" == "${NAME}" ]; then if [ ${USER_AGE} -ge ${AGE_LIMIT} ]; then if [ "${HAS_NIGHTMARES}" == "true" ]; then echo "${USER} gets nightmares, and should not see the movie" fi fi else echo "Who is this?" fi