失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Linux shell 脚本结构化命令 if-then

Linux shell 脚本结构化命令 if-then

时间:2022-07-27 23:41:45

相关推荐

Linux shell 脚本结构化命令 if-then

shell 脚本结构化命令

1. if-then 语句基本使用

if-then 语句的基本格式:

if commandthencommandselifcommandselsecommandsfi

bash 的 if 语句会运行 if 后面的命令。如果命令的退出状态码是0(命令运行正确),则运行位于 then 部分的命令。如果是其他值,执行 else 部分的命令。

zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/bashif ls dirthenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh ls: 无法访问 'dir': 没有那个文件或目录exec else commandszzz@ubuntu:~/my_learning$ vim test.sh zzz@ubuntu:~/my_learning$ zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/bashif ls .thenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh test.shexec then commandszzz@ubuntu:~/my_learning$

2. test 命令

if-then 语句判断的是命令的状态码。如果需要测试状态码以外的条件,可以使用 test 命令。如果 test 判断的条件成立 test 将返回退出状态码0,如果不成立返回非零退出状态码。test 命令的基本格式:

test condition

使用 test 命令测试变量是否包含内容,如果变量不为空执行then部分语句,否则执行else部分语句。

zzz@ubuntu:~/my_learning$ vim test.sh zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha="My_Name"if test $athenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh exec then commandszzz@ubuntu:~/my_learning$ zzz@ubuntu:~/my_learning$ zzz@ubuntu:~/my_learning$ zzz@ubuntu:~/my_learning$ vim test.sh zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha=""if test $athenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh exec else commandszzz@ubuntu:~/my_learning$

3. test 命令的另一种测试方法

这种方式,无需声明 test ,而是使用方括号,要注意左括号右边和右括号左边的空格,否则报错。基本格式如下:

if [ condition ]thencommandsfi

可以判断的三类条件:

数值比较字符串比较文件比较

3.1 数值比较

test 的数值比较只能处理整数,不能处理浮点数。

zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha=4if [ $a -eq 4 ]thenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh exec then commandszzz@ubuntu:~/my_learning$

3.2 字符串比较

注意

字符串比较时,需要注意字符串的两个顺序问题:

大于号和小于号必须使用转义字符 \,否则将被视为重定向符号;大于小于顺序和sort命令采用的不同:sort 中大号字母被认为是大于小写字母的;

3.3 文件比较

可以测试Linux文件系统上的文件和目录。

3.4 复合条件测试

布尔运算 and , or 。

[ condition1 ] && [ condition2 ][ condition1 ] || [ condition2 ]

4. if-then 高级特性

bash shell 提供了两项可在 if-then 语句中使用的高级特性:

用于数学表达式的双括号;用于高级字符串处理功能的双方括号;

4.1 双括号

zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha=4if (( $a **2 >10))thenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh exec then commandszzz@ubuntu:~/my_learning$

注:双括号中的大于小于号不需要转义。

4.2 双方括号

双括号中使用 test 测试中的标准字符串比较。另外,双方括号提供了一个特性——模式匹配,可以使用正则表达式。

zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha="my_name"if [[ $a = *name ]]thenecho "exec then commands"elseecho "exec else commands"fizzz@ubuntu:~/my_learning$ ./test.sh exec then commandszzz@ubuntu:~/my_learning$

5. case 命令

case 命令采用列表格式来检查单个变量的多个值,执行不同的命令。可以通过竖线操作在一行中分隔多个模式。*会捕获所有与已知模式不匹配的值。

基本格式:

case variable inpattern1) commands;;pattern2 | pattern3) commands;;*) default commands;;esac

zzz@ubuntu:~/my_learning$ cat test.sh #!/bin/basha="s2"case $a ins1 | s2)echo "exec s1 or s2 commands";;s3)echo "exec s3 commands";;*)echo "exec * commands";;esaczzz@ubuntu:~/my_learning$ ./test.sh exec s1 or s2 commandszzz@ubuntu:~/my_learning$

如果觉得《Linux shell 脚本结构化命令 if-then》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。