Shell script conditional test statement, if, case

1. Condition test

1.1 test command

Test whether a specific expression is true. When the condition is true, the return value of the test statement is 0, otherwise it is other values.

格式:
test  条件表达式
或【条件表达式】(条件表达式的两边都需要有空格,且此方法更为常用)

1.2 File Test

1.2.1 Common options for file testing

Common Test Operators meaning
-d test for directory
-e Test for the existence of a directory or file
-f test for file
-r Test whether the current user has permission to read
-w Test whether the current user has permission to write
-x Test whether the current user has permission to execute
-L Test whether it is a soft link file
-nt Determine whether file A is newer than file B
-ot Determine whether file A is older than file B
-ef Determine whether two files are the same file

insert image description here

insert image description here

insert image description here

insert image description here
insert image description here

1.3 Numerical comparison

格式:
【整数1  操作符  整数2】

Common Test Operators

operator meaning
-eq equal
- is not equal to
-lt xiaoyu
-gt more than the
- the less than or equal to
-ge greater than or equal to

Query whether the number of files in the current directory is greater than 10, and if so, prompt

[root@localhost ~]#ls |wc -l
15
[root@localhost ~]#test `ls |wc -l` -gt 10 && echo 文件数大于10
文件数大于10

Check whether the system memory is lower than 1024M, if it is lower, prompt

[root@localhost ~]#free -m
              total        used        free      shared  buff/cache   available
Mem:           1758         359         790          17         608        1161
Swap:          3839           0        3839
[root@localhost ~]#free -m|grep "Mem"
Mem:           1758         359         791          17         608        1161
[root@localhost ~]#free -m|grep "Mem"|awk '{print $4}'
790
[root@localhost ~]#free=`free -m|grep "Mem"|awk '{print $4}'`
[root@localhost ~]#echo $free 
790
[root@localhost ~]#test `echo $free` -lt 1024 && echo 内存不足1024M
内存不足1024M
[root@localhost ~]#

1.4 String Comparison

 格式:
【字符串1=字符串2】   # 判断是否相等
【字符串1!= 字符串2】   # 判断是否不等
【-z  字符串】                     # 判断字符串是否为空

operator

operator meaning
= string content is equal
!= The string content is different, ! sign means the opposite
-z string is empty
[root@localhost ~]#echo $LANG
zh_CN.UTF-8
[root@localhost ~]#test `echo $LANG`="us.en"&&echo 当前系统语言不是us.en,当前系统语言为`echo $LANG`
当前系统语言不是us.en,当前系统语言为zh_CN.UTF-8
[root@localhost ~]#

1.5 Logic Test

格式:
【表达式1】操作符 【表达式2】
命令1 操作符 命令2

operator:

-a或&& logical AND, and the meaning
-o or ll logical or, or meaning
logical no

Two.if statement

2.1 Single branch structure

格式
if  【条件判断式】;than
       当条件判断成立时,执行一条或多条命令
fi

If there is only one judgment to be performed, then we can use a single-branch if statement, which will only be executed when the condition is true, otherwise nothing will be executed
insert image description here

2.3 Multi-branch

if【条件判断式】;then
    当条件判断成立时,可以执行一条或多条命令
 elif【条件判断式二】;then
     当条件判断成立时,可以执行一条或多条命令
  else
       当上面条件判断都不成立时,可以执行一条或多条命令
   fi

If the data needs to be judged differently, you can use the multi-branch structure to nest
insert image description here

Three.case statement

格式;
case 变量名称 in
模式1)
   命令序列
   ;;
模式2)
     程序段
     ;;
        *)
        不包含第一个变量内容与第一个变量内容的其他程序执行段
        默认程序段
        ;;

esac

important point:

  • A case line must begin with the word "in", and each pattern must end with a single closing parenthesis
  • The double semicolon ";;" indicates the end of the command sequence
  • In the pattern string, you can use square brackets to represent a continuous range, such as "[0-9]"; you can also use | to represent or, such as a|b
  • The last) represents the default mode, where the equivalent of wildcards
    insert image description here
    insert image description here

Guess you like

Origin blog.csdn.net/fyb012811/article/details/132228530