How to implement conditional judgment in shell bash


 

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
    test expression
Combination test conditions :

  • -a: and
  • -o: or
  • !: No

Second, integer comparison:

  • -eq test if two integers are equal
  • -ne Test if two integers are not equal
  • -gt test if one number is greater than another
  • -lt test if one number is less than another
  • -ge greater than or equal to
  • -le is less than or equal to

Logical relationship between commands

  • Logical AND: &&

        The first condition is false and the second condition is not used in the judgment. The final result already has
        the first condition true, and the second condition must be judged.

  • Logical OR: ||

3. String comparison

  • == equals to have spaces on both sides
  • != not equal
  • > greater than
  • < less than

Fourth, the 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, not empty is true
  • -e FILE Test if file exists
  • -f file test whether the file is a normal file
  • -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 file is executable for current user
  • -z is empty or not, 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
  • .双分支的if语句:
复制代码
if 判断条件;then
    statement1
    statement2
    .....
    else
    statement3
    statement4
fi
复制代码

Note:
if语句进行判断是否为空
 "$name” = "" ] 
等同于

[ ! "$name" ]
[ -z "$name" ]    

Note:
使用if语句的时候进行判断如果是进行数值类的判断,建议使用let(())进行判断,对于字符串使用test[ ] or [[ ]] 进行判断
(())中变量是可以不使用$来引用的

example:表述数字范围的时候 可以使用if可以是使用case

if [ $x -gt 90 -o $x -lt 100 ]
case $x in
100)
9[0-9]) 

这个语句的意思是如果$name为空,那么X=X成立折执行下面的结果;

if [ "X$name" != "x" ]

写脚本的时候很多时候需要用到回传命令,$?如果上一个命令执行成功,回传值为0,否则为1~255之间的任何一个

  • 0为真
  • 非0为假

条件测试的写法

1、执行一个命令的结果
 if grep -q "rm" fs.sh;then 

2、传回一个命令执行结果的相反值
 if !grep -q "rm" fs.sh;then 

3、使用复合命令((算式))
 if ((a>b));then 

4、使用bash关键字 [[判断式]]
 if [[ str > xyz ]];then 

5、使用内置命令:test 判断式
 if test "str" \> "xyz";then 

6、使用内置命令:[判断式]  类似test
 if [ "str" \> "xyz" ];then 

7、使用-a -o进行逻辑组合
 [ -r filename -a -x filename ] 

8、命令&&命令
 if grep -q "rm" fn.sh && [ $a -lt 100 ];then 

9、命令||命令
 if grep -q "rm" fn.sh || [ $a -lt 100 ];then 

示例脚本(

写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分

成绩(A-F)。

A: 90–100

B: 80–89

C: 70–79

D: 60–69

F: <60

)    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325678146&siteId=291194637