Tag: Shell
1. Use the if-then statement
In structured commands, the most basic type is the if-then statement. The if-then statement has the following format:
If commandthen commandsfi
If you are using the if-then statement in other programming languages, this form may confuse you. In other programming languages, the object after the if statement is an equality to test whether the value is true or false. If the exit status code of the command is 0 (the command is successfully run), the command in the then part will be executed. If the exit status code of this command is another value, the then commands will not be executed, and bash shell will continue to execute the next command in the script.
Here is a simple example to explain this concept:
$ Cat if_then.sh
#! /Bin/baslif Test 1 = 2 then Echo "It worked" fi
At this time, running if_then will not print 'it worked'. If it is changed to the following, the echo "It worked" after then will be executed.
#! /Bin/baslif Test 1 = 1 then Echo "It worked" fi