Shell operation and maintenance combat 2-condition judgment and function

Shell conditional tests and comparisons


Script Conditional Test

# -f判断文件是否存在,如果存在返回真,反之为假
# 一下结构类似于三元运算符,文件存在则执行true,不存在则执行false
test -f file && echo true || echo false

# 使用中括号包裹test所有参数及其对应值
# 中括号左右端都必须预留一个空格距离,否则报错
test [ -f file ] && echo 1 || echo 2

In general, it is recommended that there is no distance between operation symbols (that is, there are not many spaces between them)


text test expression

insert image description here

# 对单个文件变量进行测试
[ -f "$fileval" ] && echo 1 || echo 0

Conditional judgment statement templates commonly used in actual development

If the condition returns true, execute all instructions inside the braces

[ 条件 ] &&{
    命令1
    命令2
    命令3
}

string test expression

Pay special attention to the arrangement of space spacing when using it!

# -n "abs" 若字符串长度非0,返回true
[ -n "abs" ] && echo 1 || echo 0

# -z "abs" 若字符串长度为0,返回true
[ -n "" ] && echo 1 || echo 0

integer binary comparison operator

insert image description here

There are two situations when writing, and use it according to the above figure

Literal comparison symbols can also be used within and [[]], but are not recommended!(())

[ 2 -eq 1 ] && echo 1 || echo 0
[[ 2 > 10 ]] && echo 1 || echo 0

(($num1<$num2)) && echo 1 || echo 0

It is not recommended to use it in official work [[]], because it is more complicated, which will cause a lot of trouble for development

Summary of symbols used in the three forms

  • The comparison of integers plus double quotes is correct.
  • [[]]-eqIt is correct to use similar writing in middle, but it may be wrong to [[]]use similar >、<writing in middle. It is possible that only the first digit will be compared, and the logical result will be wrong.
  • []Although the similar >、<wording in 中 may be grammatically correct, but the logical result is wrong, you can use =、! =the correct comparison.
  • (())You can't use similar writing in -eq, you can use similar >、<writing.

logical operator

For AND or NOT operations, except that character expression can be used []in , the other two can use corresponding original symbols (such as &&)

[ 1 -a 1 ] # 相当于 &&
[ 1 -o 1 ] # 相当于 ||
[ 1 ! 1 ] # 相当于 !

Difference summary

insert image description here


if statement


# 基础判断语句
# if开头,then衔接,fi结尾
if  <条件表达式>
    then
    指令
fi

# 双分支结构
if  <条件表达式>
    then
    指令集1
else
    指令集2
fi

# 多分支结构
if  <条件表达式1>
    then
    指令1
elif <条件表达式2>
    then
    指令2
else
    指令3
fi

In addition, if can also be used in combination 测试表达式test、[]、[[]]、(())as a judgment condition


Shell functions


Basic execution

There are three ways to define functions in the shell

# 标准格式
function demo1(){}

# 有function修饰的情况下可不写小括号
function demo2{}

# 无function修饰必须要有小括号
demo3(){}

Pay special attention, due to the conflict between sh and bash in ubuntu, you must use the bash command when executing the script, otherwise you will always report an error when you use function to define a function! ! !


Guess you like

Origin blog.csdn.net/delete_you/article/details/130747818