Shell programming, conditional statements

Conditional test operation

The Shell environment judges whether the execution is successful according to the return status value ($?) after the command is executed. When the return value is 0 (true true), it means success, and when the return value is non-zero (false false), it means failure or abnormality.

test command

You can test specific conditions and determine whether the condition is established according to the return value ($? return value of 0 indicates that the condition is established)

format

格式一:test 条件表达式
格式二:[ 条件表达式 ]			
#方括号“[”或“]”与条件表达式之间需要至少一个空格进行分隔

Insert picture description here
Insert picture description here

File test

format

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

Commonly used test operators

-e: Test whether the directory or file exists (Exist).
-d: Test whether it is a directory (Directory).
-f: Test whether it is a file (File).
-r: Test whether the current user has permission to read (Read).
-w: Test whether the current user has permission to write (Write).
-x: Test whether the executable (Excute) permission is set.
Insert picture description here
Insert picture description here

Integer comparison

format

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

Commonly used operators

-eq:等于					==
-ne:不等于					!=
-gt:大于					\>
-lt:小于					\<
-le:小于等于
-ge:大于等于

Insert picture description here
Insert picture description here

String comparison

Format 1:

[  字符串1  =  字符串2 ][  字符串1  ==  字符串2 ] 
[  字符串1  !=  字符串2 ]

Insert picture description here
Insert picture description here

Insert picture description here

Format 2:

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

Insert picture description here

Logic test

Commonly used operators:

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

format

格式1[  表达式1  ]  操作符  [  表达式2  ]  &&||
格式2:命令1  操作符  命令2    -a和 -o

Insert picture description here

Supplementary format

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

Insert picture description here

Supplementary execution order

And when you are with or together, only one will be executed. If the condition is met, it will be calculated or the previous one, if it is not true, it will be calculated or
Insert picture description here
the next experiment of pinging a website will
Insert picture description here
Insert picture description here
not be thrown into the black hole.
Insert picture description here
Insert picture description here

if statement

Single branch structure

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

Insert picture description here

Single branch format

Insert picture description here

Double branch structure

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

Insert picture description here

Double branch format

Insert picture description here
Insert picture description here
Insert picture description here

Multi-branch structure

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

Insert picture description here
Insert picture description here
Insert picture description here

case statement structure

case multi-branch structure

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

Insert picture description here

format

Insert picture description here
Insert picture description here

Supplement-regular expression

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Jun____________/article/details/114327595