欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > shell:使用结构化语句(控制流)

shell:使用结构化语句(控制流)

2024/10/23 23:22:40 来源:https://blog.csdn.net/weixin_43999327/article/details/139962986  浏览:    关键词:shell:使用结构化语句(控制流)

许多程序要求对shell脚本中的命令施加一些逻辑流程控制。有一类命令会根据条件使脚本跳
过某些命令。这样的命令通常称为结构化命令(structured command)。

1. if-then、if-then-else、if-then-elif-else

如果该命令的退出状态码是0 (该命令成功运行),位于then部分的命令就会被执行。

1.1 if-then

格式:

# 1. 基础格式
if command 
thencommands
fi# 2. 基础格式2
if command; thencommands
fi

1.2 if-then-else

if-then-else语句在语句中提供了另外一组命令。

if command; thencommands
elsecommands
fi

1.3 嵌套if

if command1; thencommands 
elif command2; thenmore commands 
fi

1.4 if-then-elif-else

if command1; thencommands 
elif command2; thencommands
elsecommands
fi

2. test命令

test命令提供了在if-then语句中测试不同条件的途径。如果test命令中列出的条件成立,
test命令就会退出并返回退出状态码0。

test命令的格式非常简单。

test condition

condition是test命令要测试的一系列参数和值。当用在if-then语句中时,test命令看 起来是这样的。

if test condition;then
commands
fi

样例:

#!/bin/bash
# Testing the test command #
my_variable="Full"
#
if test $my_variable
thenecho "The $my_variable expression returns a True"
#
elseecho "The $my_variable expression returns a False"
fi

变量my_variable中包含有内容(Full),因此当test命令测试条件时,返回的退出状态 为0。这使得then语句块中的语句得以执行。

3. test优化写法

bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。

if [ condition ] then
commands
fi

方括号定义了测试条件。注意,第一个方括号之后和第二个方括号之前必须加上一个空格
否则就会报错。

test命令可以判断三类条件:

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

3.1 数值比较

在这里插入图片描述

3.2 字符串比较

在这里插入图片描述
用例:

$ cat test10.sh #!/bin/bash
# testing string length val1=testing
val2=''
#
if [ -n $val1 ]
thenecho "The string '$val1' is not empty"elseecho "The string '$val1' is empty"fi#if [ -z $val2 ]thenecho "The string '$val2' is empty"elseecho "The string '$val2' is not empty"fi#if [ -z $val3 ]thenecho "The string '$val3' is empty"elseecho "The string '$val3' is not empty"
fi
$
$ ./test10.sh
The string 'testing' is not empty The string '' is empty
The string '' is empty
$

3.3 文件比较

在这里插入图片描述

4. 复合条件

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:

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

第一种布尔运算使用AND布尔运算符来组合两个条件。要让then部分的命令执行,两个条件都必须满足。

第二种布尔运算使用OR布尔运算符来组合两个条件。如果任意条件为TRUE,then部分的命 令就会执行。

5. if-then的高级特性

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

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

5.1 使用双括号

双括号命令允许你在比较过程中使用高级数学表达式。双括号命令的格式如下:

(( expression ))

在这里插入图片描述

5.2 使用双方括号

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:

[[ expression ]]

双方括号里的expression使用了test命令中采用的标准字符串比较。但它提供了test命
令未提供的另一个特性——模式匹配(pattern matching)。

用例:

$ cat test24.sh
#!/bin/bash
# using pattern matching #4 if [[ $USER == r* ]]
then
echo "Hello $USER"
else 5echo "Sorry, I do not know you"fi
$ ./test24.sh 6 Hello rich
$

6. case命令

case命令会采用列表格式来检查单个变量的多个值。

case variable in
pattern1 | pattern2) commands1;; 
pattern3) commands2;;
*) default commands;;
esac

case命令会将指定的变量与不同模式进行比较。如果变量和模式是匹配的,那么shell会执行 为该模式指定的命令。可以通过竖线操作符在一行中分隔出多个模式模式。星号会捕获所有与已 知模式不匹配的值。

这里有个将if-then-else程序转换成用case命令的例子。

$ cat test26.sh #!/bin/bash
# using the case command #
case $USER in
rich | barbara)echo "Welcome, $USER"echo "Please enjoy your visit";;testing)echo "Special testing account";;jessica)echo "Do not forget to log off when you're done";;*)echo "Sorry, you are not allowed here";;esac
$
$ ./test26.sh
Welcome, rich
Please enjoy your visit 
$

case命令提供了一个更清晰的方法来为变量每个可能的值指定不同的选项。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com