Conditional expressions of shell programming
There are three forms of statements that can be used as expressions of if statements in linux
1. Command form
If the command is executed successfully, it returns 0 (such as grep, a match is found) and if
the execution fails, it returns non-zero (grep, no match is found)
if commend
then codes
fi
example
read -p"输入用户名:" name
if id -u $name > /dev/null 2>&1#
then echo "$name exist"
else
echo "$name does not exist!"
Enter username: adins
adins does not exist
2. Functional form
If the function executes successfully, it returns true, and if it fails, it returns false.
#!/bin/bash
fun(){
echo "adins"
}
if fun
then echo test
fi
Three, test, [] form
In fact, the expressions in the form of test and [] have the same effect.
Recommend a blog: Linux Bash Shell Programming (8): Conditional Judgment and Examples