http://blog.51cto.com/lovelace/1211353
How to implement conditional judgment in bash?
Condition Test Type:
Integer Test
Character Test
File Test
1. Expression of conditional test:
[ expression ] There must be spaces at both ends of the brackets
[[ expression ]] There must be spaces at both ends of the brackets
test expression
Combination test conditions:
-a: and
-o: or
!: not
2. Integer comparison:
-eq test whether two integers are equal
-ne test whether two integers are not equal
-gt test whether one number is greater than the other
-lt test whether one number is less than the other
-ge is greater than or equal to
-le is less than Or is equal to
the logical relationship between commands.
Logical AND: &&
The first condition is false The second condition does not need to be judged, and the final result already has
the first condition to be true, and the second condition must be judged.
Logical OR: ||
Three , String comparison
String comparison:
== equal to have spaces on both sides
!= not equal
> greater than
< less than
four, file test
-z string test whether the specified character is empty, empty is true, non-empty is false
-n string test whether the specified string is not empty, empty is false non-empty is true
-e FILE test whether the file exists
-f file test whether the file For ordinary files
-d file Test whether the specified path is a directory
-r file Test whether the file is readable by the current user
-w file Test whether the file is writable by the current user
-x file Test whether the file is executable by the current user
-z Whether it is true if empty - a
is not empty
Five, if syntax
if judging condition 0 is true, others are false
Single branch if statement
if judgment condition; then
statement1
statement2
......
fi
Double branch if statement:
if judgment condition; then
statement1
statement2
.....
else
statement3
statement4
fi
Note:
The if statement to determine whether it is empty
[ "$name" = "" ] is
equivalent to
[ ! "$name" ]
[ -z "$name" ]
Note:
Judgment when using if statement If it is a numerical class, it is recommended to use let(()) to judge.
For strings, etc., use test[ ] or [[ ]] to judge
the variables in (()) can not be used $ to refer to
example: when expressing a range of numbers, you can use if, you can use case
if [ $x -gt 90 -o $x -lt 100 ]
case $x in
100)
9[0-9])
The statement if [ "X$name" != "x" ]
means that if $name is empty, then X=X is established and the following result is executed;
When writing scripts, you often need to use the return command. $? If the previous command was executed successfully, the return value is 0, otherwise any
0 between 1 and 255 is true and
not 0 is false.
Writing a conditional test:
1. The result of executing a command
if grep -q "rm" fs.sh;then
2. Return the opposite value of the execution result of a command
if! grep -q "rm" fs.sh;then
3. Use compound commands ((calculation))
if ((a>b));then
4. Use the bash keyword [[judgment]]
if [[ str > xyz ]];then
5. Use the built-in command: test judgment
if test "str" \> "xyz";then
6. Use built-in commands: [judgment] similar to test
if [ "str" \> "xyz" ];then
7. Use -a -o for logical combination
[ -r filename -a -x filename ]
8、命令&&命令
if grep -q "rm" fn.sh && [ $a -lt 100 ];then
9. Command || Command
if grep -q "rm" fn.sh || [ $a -lt 100 ];then
example script (
Write a script, input a test score, and output his score according to the following criteria
Grades (AF).
A: 90–100
B: 80–89
C: 70–79
D: 60–69
F: <60
)
#/bin/bash #Verson:0.1 #Auther:lovelace #Pragram:This pragram is calculation your grade #import an argument read -p "Please input your grade:" x declare -i x #jugemet $x value is none or not if [ "$x" == "" ];then echo "You don't input your grade...." exit 5 be #jugement the gread level if [[ "$x" -ge "90" && "$x" -le "100" ]];then echo "Congratulation,Your grade is A." elif [[ "$x" -ge "80" && "$x" -le "89" ]];then echo "Good,Your grade is B." elif [[ "$x" -ge "70" && "$x" -le "79" ]];then echo "Ok.Your grade is C." elif [[ "$x" -ge "60" && "$x" -le "69" ]];then echo "Yeah,Your grade is D." elif [[ "$x" -lt "60" ]];then echo "Right,Your grade is F." else echo "Unknow argument...." be