Shell Programming - determination condition (2)

Shell programming of conditional

Condition test operation

test command

Test specific expression is established when the condition is met, the test statement returns 0, otherwise other values

格式1:test 条件表达式
格式2:[ 条件表达式 ]

Test file
[file or directory operator]
Common test operators
-d: Test whether the directory
-e: test directory or file exists
-f: test whether the file
-r: test whether the current user has permission to read
-w : test whether the current user has permission to write
the -X-: test whether the current user is authorized to perform
Shell Programming - determination condition (2)
integer comparison
[1 integer operator integer of 2]
common test operators
-eq: equal
-ne: no equal
-gt: greater than
-lt : less than
-le: less than or equal to
-ge: greater than or equal to the
Shell Programming - determination condition (2)
string comparison
format 1: [string string 1 = 2]
[! 1 string string = 2]
format 2: [string the -Z]
Shell Programming - determination condition (2)
logic test
format 1: [expression 1] operator [expression 2] ...
format 2: command 1 command 2 ... operator
common test operator
-a or &&: logical aND, "and" means
-o or ||: logical or, "or" means
:! logical negation
Shell Programming - determination condition (2)

If the structure of the sentence

Single-branch structure

if                          //条件测试操作
    then                  //命令序列
fi                        //结束命令序列判断

Shell Programming - determination condition (2)

脚本实例:
#!/bin/bash
#比较输入的整数大不大
read -p "请输入一个整数:" num
if [ $num -ge 5 ]
 then
        echo "这个数大"
fi

Shell Programming - determination condition (2)

Two-branch structure

if                               //条件测试操作
    then                      //命令序列1
    else                    //命令序列2
fi                         //结束命令序列判断

Shell Programming - determination condition (2)

脚本实例:
#!/bin/bash
#比较输入的整数大不大
read -p "请输入一个整数:" num
if [ $num -ge 5 ]
 then
        echo "这个数大"
 else
        echo "这个数小"
fi

Shell Programming - determination condition (2)

Multi-branch structure

if                             //条件测试操作1
    then                      //命令序列1
elif                          //条件测试操作2
    then                     //命令序列2else            
                               //命令序列3
fi                            //结束命令序列判断

Shell Programming - determination condition (2)

#!/bin/bash
read -p "请输入你的成绩:" num
if [ $num -gt 85 ] && [ $num -le 100 ]
 then
        echo "优秀"
elif [ $num -gt 60 ] && [ $num -le 85 ]
 then
        echo "良好"
 else
        echo "不及格"
fi

Shell Programming - determination condition (2)

Nested if statements

#!/bin/bash
read -p "请输入你的比赛时间:" num
if [ $num -lt 10 ]
 then
        echo "恭喜你进入决赛"
 read -p "您的性别是:" sex
 if [ $sex = "男" ]
  then
        echo "您进入男子组"
 else
        echo "您进入女子组"
 fi
else
        echo "抱歉你无缘决赛"
fi

Shell Programming - determination condition (2)

thanks for reading! ! !

Guess you like

Origin blog.51cto.com/14080162/2440562