SoFunction
Updated on 2025-04-14

shell structured command if-then-else statement

In the if-then statement, you have only one choice regardless of whether the command is successfully executed. If the command returns a non-0 exit status code, the bash shell will continue to execute the next command in the script. In this case, it would be great if another set of commands could be executed. This is exactly what the if-then-else statement does.

The if-then-else statement provides another set of commands in the statement:

if command
then 
       command
else
        command
fi

When the command in the if statement returns the exit status code 0, the command in the then part will be executed, which is the same as the ordinary if-then statement. When the command in the if statement returns a non-0 exit status code, the bash shell will execute the command in the else part.

The shell script first determines whether the file test1 is readable. If so, the output is readable! prompt message; otherwise, no action will be performed.

[root@localhost 20190105]# vi 
filename=test1
if [ -r $filename ]            //Output test1 can be readable and output informationthen
echo $filename' is readable !'
fi
[root@localhost 20190105]# sh 
test1 is readable !

The shell script will determine whether the number variable is equal to 100. If so, the output The number is equal 100! prompt; otherwise, the number is not equal 100 !.

[root@localhost 20190105]# vi 
number=200
if [ $number -eq 100 ]                 //If number is equal to 100, output "The number is equal 100!" promptthen
       echo 'The number is equal 100 !'
else                            //Otherwise, the prompt output is "The number is not equal 100!"       echo 'The number is not equal 100 !'
fi
[root@localhost 20190105]# sh 
The number is not equal 100 !

The shell script first determines whether the number variable is less than 10, and if so, the output is The number < 10 !; otherwise, determines whether the number variable is greater than or equal to 10 and less than 20.

If so, the output is 10 =< The number < 20 !; otherwise, determine whether the number variable is greater than or equal to 20 and less than 30.

If yes, output 20 =< The number < 30 !; otherwise, output 30 <= The number! .

[root@localhost 20190105]# vi 
number=25
if [ $number -lt 10 ]              //If number is less than 10then
       echo 'The number &lt; 10 !'
elif [ $number -ge 10 -a $number -lt 20 ] //If number is greater than or equal to 10 and less than 20then
       echo '10 =&lt; The number &lt; 20 !'
elif [ $number -ge 20 -a $number -lt 30 ] //If number is greater than or equal to 20 and less than 30then
       echo '20 =&lt; The number &lt; 30 !'
else                         //Other situations except for the above three situations       echo '30 &lt;= The number !'
fi
[root@localhost 20190105]# sh 
20 =&lt; The number &lt; 30 !

Now you can copy and modify the test script and add the else section:

$ cp  
$
$ nano 
$
$ cat 
#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The scropt files in the home directory of $testuser are:"
    ls /home/$testuser/*.sh
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi
echo "We are outside the if statement"
$
$ ./
The user NoSuchUser does not exist on this system.

We are outside the if statement

This makes it much more friendly. Like the then part, the else part can contain multiple commands. The fi statement indicates that the else part ends.

This is all about this article about the shell structured command if-then-else statement. For more relevant shell if-then-else content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!