Conditional statements of Shell programming (detailed graphic interpretation)

Conditional statements of Shell programming (detailed graphic interpretation)

1. Conditional test statement

Shell环境根据命令执行后的返回状态值($?)来判断是否执行成功,当返回值为0(真true)时表示成功,返回值为非0值(假false)时表示失败或异常。
test 命令,可以对特定条件进行测试,并根据返回值来判断条件是否成立($?返回值为 0 表示条件成立)。
格式一:test 条件表达式
格式二:[ 条件表达式 ]			#方括号“[”或“]”与条件表达式之间需要至少一个空格进行分隔

(1) File testing

格式:[ 操作符 文件或目录 ]

常用的操作符:
-e:测试目录或文件是否存在(Exist)。
-d:测试是否为目录(Directory)。
-f:测试是否为文件(File)。
-r:测试当前用户是否有权限读取(Read)。
-w:测试当前用户是否有权限写入(Write)。
-x:测试是否设置有可执行(Excute)权限。

(Two), integer value comparison

格式:[ 整数变量1 操作符 整数变量2 ]

常用的操作符:
-eq:等于					==
-ne:不等于					!=
-gt:大于					\>
-lt:小于					\<
-le:小于等于
-ge:大于等于

Insert picture description here

Insert picture description here

如果需要算出空闲比,就需要浮点运算符,bash不支持浮点运算,需要借助bc、awk
bc  例  加:echo "1.1+2.2" | bc

​        减:echo "5.3-4.6" | bc

​        乘:echo "2.3*1.4"| bc

​        除:echo "scale=2; 4.3/2.5"|bc

   混合运算:echo "scale=2; 2.2/(1.2+2.3)*1.5+2.1" | bc

Insert picture description here
Insert picture description here

awk 例:         加:echo $(awk 'BEGIN{print 4.5+3.4}')

​                减:echo $(awk 'BEGIN{print 4.5-3.4}')

​                乘:echo $(awk 'BEGIN{print 4.5*3.4}')

​                除:echo $(awk 'BEGIN{print 4.5/3.4}')

Insert picture description here

算出内存空闲比:F=`free -m | grep "Mem:" | awk '{print$4/$2*100}'`

​                                 echo ${
    
    F}%

Insert picture description here

(Three), string comparison

格式1[  字符串1  =  字符串2 ][  字符串1  ==  字符串2 ]   #表示字符串1跟字符串2相同
[  字符串1  !=  字符串2 ]    # 表示字符串1跟字符串2 不同

格式2[  -z  字符串 ]		#检查字符串是否为空(Zero),对于未定义或赋予空值的变量将视为空串
[  -n  字符串 ]		#检查是否有字符串存在 

Insert picture description here

(Four), logic test

格式1[  表达式1  ]  操作符  [  表达式2  ]  
格式2[命令1  操作符  命令2]

在同一个[] 中括号内,使用-a -o ,不同的[]中括号,需要使用&&|| 

常用的操作符:
-a或&& :逻辑与,“而且”的意思,前后条件需都成立
-o或|| :逻辑或,“或者”的意思,只需前后条件中一个成立
! :逻辑否

Insert picture description here

&&||操作符能够正常存在于[[ ]]条件判断结构中,但是如果出现在[ ]结构中的话,会报错

Insert picture description here
Insert picture description here

Two, if statement

(1) Single branch structure of if statement

if  条件测试操作
    then   命令序列      
fi

Insert picture description here

Example: Determine the mount point directory and create it automatically if it does not exist:

Insert picture description here
Insert picture description here

(Two), double branch if statement

if 条件测试操作
then
命令序列 1
else
命令序列 2
fi

Insert picture description here

Example: Determine whether the target host is alive and display the detection result

Insert picture description here

Insert picture description here

(Three), multi-branch if statement

if 条件测试操作 1
then
命令序列 1
elif 条件测试操作 2
then
命令序列 2
[else]
[命令序列 3]
fi

Insert picture description here

Example: Judging the range of scores and dividing into three grades: excellent, qualified and unqualified

Insert picture description here

Insert picture description here

Three, case statement

case 变量值 in
模式 1)
命令序列 1
;;
模式 2)
命令序列 2
;;
……

*)
默认命令序列
esac

Insert picture description here

Example: Judging the range of scores and dividing into three grades: excellent, qualified and unqualified

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/111304096