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

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

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

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

1.1 if-then

格式:

# 1. 基础格式
if command 
then
	commands
fi

# 2. 基础格式2
if command; then
	commands
fi

1.2 if-then-else

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

if command; then
	commands
else
	commands
fi

1.3 嵌套if

if command1; then
	commands 
elif command2; then
	more commands 
fi

1.4 if-then-elif-else

if command1; then
	commands 
elif command2; then
	commands
else
	commands
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
then
   echo "The $my_variable expression returns a True"
#
else
   echo "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 ]
then
       echo "The string '$val1' is not empty"
    else
       echo "The string '$val1' is empty"
    fi
    #
    if [ -z $val2 ]
    then
       echo "The string '$val2' is empty"
    else
       echo "The string '$val2' is not empty"
    fi
    #
    if [ -z $val3 ]
    then
       echo "The string '$val3' is empty"
    else
       echo "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 5
       echo "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命令提供了一个更清晰的方法来为变量每个可能的值指定不同的选项。

  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值