12.1 使用 if-then 语句

格式:

  • if 检测其后 command 的退出状态码
  1. if command
  2. then
  3. commands
  4. fi
  5. if command; then
  6. commands
  7. fi

使用:

#!/bin/bash
# testing the if statement
if pwd
then
    echo "It worked"
fi

12.2 if-then-else 语句

格式:

if command
then
    commands
else
    commands
fi

使用:

#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The bash files for user $testuser are:"
    ls -a /home/$testuser/.b*
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi

12.3 嵌套 if

#!/bin/bash
# Testing nested ifs
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The user $testuser exists on this system."
else
    echo "The user $testuser does not exist on this system."
    if ls -d /home/$testuser/
    then
        echo "However, $testuser has a directory."
    fi
fi

else 部分的另一种形式: elif

if command1
then
    commands
elif command2
then
    more commands
fi

使用:

#!/bin/bash
# Testing nested ifs - use elif
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The user $testuser exists on this system."
#
elif ls -d /home/$testuser
then
    echo "The user $testuser does not exist on this system."
    echo "However, $testuser has a directory."
fi

以 else 结束:

  • else 代码块属于紧挨着的 elif
#!/bin/bash
# Testing nested ifs - use elif
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd
then
    echo "The user $testuser exists on this system."
#
elif ls -d /home/$testuser
then
    echo "The user $testuser does not exist on this system."
    echo "However, $testuser has a directory."
#
else
    echo "The user $testuser does not exist on this system."
    echo "And, $testuser does not have a directory."
fi

12.4 test 命令

格式:

  • 没有 condition 的 test 返回非0退出状态码
test condition

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

使用 test 的简便方式:

  • 左方括号后与右方括号前必须有空格
if [ condition ]
then
    commands
fi

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

  • 数值
  • 字符串
  • 文件

12.4.1 数值比较

条件参数:

image.png

使用:

  • 不能比较浮点数
#!/bin/bash
# Using numeric test evaluations
#
value1=10
value2=11
#
if [ $value1 -gt 5 ]
then
    echo "The test value $value1 is greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
    echo "The values are equal"
else
    echo "The values are different"
fi
#

12.4.2 字符串比较

字符串比较相对繁琐:

image.png

1. 字符串相等性

2. 字符串顺序

注意:

  • 大于号/小于号必须转义
  • 字符串比较规则

3. 字符串大小

12.4.3 文件比较

测试文件操作符:

image.png

1. 检查目录

#!/bin/bash
# Look before you leap
#
jump_directory=/home/arthur
#
if [ -d $jump_directory ]
then
    echo "The $jump_directory directory exists"
    cd $jump_directory
    ls
else
    echo "The $jump_directory directory does not exist"
fi
#

2. 检查对象是否存在

3. 检查文件

4. 检查是否可读

5. 检查空文件

6. 检查是否可写

7. 检查文件是否可以执行

8. 检查所属关系

9. 检查默认属组关系

10. 检查文件日期

12.5 复合条件测试

格式:

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

使用:

#!/bin/bash
# testing compound comparisons
#
if [ -d $HOME ] && [ -w $HOME/testing ]
then
    echo "The file exists and you can write to it"
else
    echo "I cannot write to the file"
fi

12.6 if-then 的高级特性

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

12.6.1 使用双括号

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

格式:

(( expression ))

双括号表达式支持其它运算符:

image.png

使用:

#!/bin/bash
# using double parenthesis
#
val1=10
#
if (( $val1 ** 2 > 90 ))
then
    (( val2 = $val1 ** 2 ))
    echo "The square of $val1 is $val2"
fi

12.6.2 使用双方括号

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

  • 不是所有的 shell 都支持双方括号
  • 提供模式匹配功能 (pattern matching). 与正则配合使用

格式:

[[ expression ]]

使用:

#!/bin/bash
# using pattern matching
#
if [[ $USER == r* ]]
then
    echo "Hello $USER"
else
    echo "Sorry, I do not know you"
fi

12.7 case 命令

格式:

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

使用:

#!/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

12.8 小结