congdong007

Penetration Test、Software Developer

0%

Bash Scripting 05 - If, Else, Elif Statements

Conditional statements allow us to perform different actions based on different conditions. The
most common conditional Bash statements include if, else, and elif.
The if statement is relatively simple–it checks to see if a condition is true–but it requires a very
specific syntax. Pay careful attention to this syntax, especially the use of required spaces:

1
2
3
4
if [ <some test> ]
then
<perform an action>
fi

In this listing, if “some test” evaluates as true, the script will “perform an action”, or any commands
between then and fi. Let’s look at an actual example:

1
2
3
4
5
6
7
8
9
10
11
12
kali@kali:~$ cat ./if.sh
#!/bin/bash
# if statement example
read -p "What is your age: " age
if [ $age -lt 16 ]
then
echo "You might need parental permission to take this course!"
fi
kali@kali:~$ chmod +x ./if.sh
kali@kali:~$ ./if.sh
What is your age: 15
You might need parental permission to take this course!

In this example, we used an if statement to check the age entered by a user. If the entered age was
less than (-lt) 16, the script would output a warning message.
The square brackets (“[“ and “]”) in the if statement above are actually a reference to the test
command. This simply means we can use all of the operators that are allowed by the test
command. Some of the most common operators include:

Operator Description: Expression True if…
!EXPRESSION The EXPRESSION is false.
-n STRING STRING length is greater than zero
-z STRING The length of STRING is zero (empty)
STRING1 != STRING2 STRING1 is not equal to STRING2
STRING1 = STRING2 STRING1 is equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER 2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER 2
-d FILE FILE exists and is a directory
-e FILE FILE exists
-r FILE FILE exists and has read permission
-s FILE FILE exists and it is not empty
-w FILE FILE exists and has write permission
-x FILE FILE exists and has execute permission

With the above in mind, our previous example using if can be rewritten without square brackets as
follows:

1
2
3
4
5
6
7
8
9
10
11
12
kali@kali:~$ cat ./if2.sh
#!/bin/bash
# if statement example 2
read -p "What is your age: " age
if test $age -lt 16
then
echo "You might need parental permission to take this course!"
fi
kali@kali:~$ chmod +x ./if2.sh
kali@kali:~$ ./if2.sh
What is your age: 15
You might need parental permission to take this course!

Even though this example is functionally equivalent to the example using square brackets, using
square brackets makes the code slightly easier to read.
We can also perform a certain set of actions if a statement is true and another set if it is false. To
do this, we can use the else statement, which has the following syntax:

1
2
3
4
5
6
if [ <some test> ]
then
<perform action>
else
<perform another action>
fi

Let’s extend our previous “age” example to include the else statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
kali@kali:~$ cat ./else.sh
#!/bin/bash
# else statement example
read -p "What is your age: " age
if [ $age -lt 16 ]
then
echo "You might need parental permission to take this course!"
else
echo "Welcome to the course!"
fi
kali@kali:~$ chmod +x ./else.sh
kali@kali:~$ ./else.sh
What is your age: 21
Welcome to the course!

Notice that the else statement was executed when the entered age was greater than (or more
specifically “not less than”) sixteen.
The if and else statements only allow two code execution branches. We can add additional
branches with the elif statement which uses the following pattern:

1
2
3
4
5
6
7
8
9
if [ <some test> ]
then
<perform action>
elif [ <some test> ]
then
<perform different action>
else
<perform yet another different action>
fi

Let’s again extend our “age” example to include the elif statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
kali@kali:~$ cat ./elif.sh
#!/bin/bash
# elif example
read -p "What is your age: " age
if [ $age -lt 16 ]
then
echo "You might need parental permission to take this course!"
elif [ $age -gt 60 ]
then
echo "Hats off to you, respect!"
else
echo "Welcome to the course!"
fi
kali@kali:~$ chmod +x ./elif.sh
kali@kali:~$ ./elif.sh
What is your age: 65
Hats off to you, respect!

In this example, the code execution flow was slightly more complex. In order of operation, the then
branch executes if the entered age is less than sixteen, the elif branch is entered (and the “Hats
off..” message displayed) if the age is greater than sixty, and the else branch executes only if the
age is greater than sixteen but less than sixty.