Unlike Java, PHP and other languages, the process control of sh cannot be empty, such as:
Copy the codeThe code is as follows:
<?php
if (isset($_GET["q"])) {
search(q);
}
else {
//do nothing
}
?>
This is not possible in sh/bash. If the else branch does not have a statement to execute, don't write this else, just like this:
Copy the codeThe code is as follows:
if condition
then
command1
command2
...
commandN
fi
Of course, it can also be written as a line (applicable to terminal command prompts), like this:
Copy the codeThe code is as follows:
if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;
The fi at the end is the spelling of if and the other ones will be encountered later.
if else format
Copy the codeThe code is as follows:
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else format
Copy the codeThe code is as follows:
if condition1
then
command1
elif condition2
command2
else
commandN
fi
The if else statement is often used in conjunction with the test command, as shown below:
Copy the codeThe code is as follows:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
Output:
The two numbers are equal!