1. if-then 语句
最基本的结构化命令就是
if-then
语句。if-then
语句有如下格式。bash shell的 if 语句会运行 if 后面的那个命令。如果该命令的退出状态码是 0即该命令运行成功,位于 then 部分的命令就会被执行。如果该命令的退出状态码是其他值, then部分的命令就不会被执行。fi
语句用来表示 if-then语句到此结束。
if command
then
commands
fi
语句形式还可以写成这样
if command; then
commands
fi
2. if-then-else 语句
在
if-then
语句中,不管命令是否成功执行,你都只有一种选择。如果命令返回一个非零退出状态码,bash shell会继续执行脚本中的下一条命令。在这种情况下,如果能够执行另一组命令就更好了。这正是 if-then-else 语句的作用。
if command
then
commands
else
commands
fi
3. 嵌套 if 语句
嵌套 if 语句并不是新的语句格式,只不过在 if 语句内部中再次,或多次使用 if 语句。
if command1
then
commands
else
if command2
then
commands
fi
fi
4. 多分支 if-elif 语句
if-else
最多只能应对两种情况,但是很多时候你需要应对更多的情形,所以在 shell 编程中可以使用if-elif
和if-elif-else
语句。
if command1
then
commands
elif command2
then
others commands
fi
5. test 命令
在
if
语句中看到的都是普通 shell 命令。if-then
语句不能测试命令退出状态码之外的条件。所以在 bash shell 中提供了test
命令帮你通过if-then
语句测试其他条件。test 命令可以判断三类条件:
- 数值比较
- 字符串比较
- 文件比较
test 命令的格式如下:
test
condition
test 命令配合 if
格式如下:
if test condition
then
commands
fi
5.1 数值比较
使用
test
命令最常见的情形是对两个数值进行比较。表12-1列出了测试两个值时可用的条件
参数。
test 命令的数值比较功能
----------+-----------------------------
比 较 | 描 述
----------+-----------------------------
n1 -eq n2 | 检查 n1 是否与 n2 相等
n1 -ge n2 | 检查 n1 是否大于或等于 n2
n1 -gt n2 | 检查 n1 是否大于 n2
n1 -le n2 | 检查 n1 是否小于或等于 n2
n1 -lt n2 | 检查 n1 是否小于 n2
n1 -ne n2 | 检查 n1 是否不等于 n2
------------------+---------------------
例如,大于:
a=6
if [ $a -gt 5 ]
then
echo "a>5"
fi
例如,等于:
a=5
if [ $a -eq 5 ]
then
echo "a=5"
else
echo "a!=5"
fi
例如,小于:
a=3
if [ $a -lt 5 ]
then
echo "a<5"
fi
5.2 字符串比较
字符串比较测试命令
-------------+----------------------------
比 较 | 描 述
-------------+----------------------------
str1 = str2 | 检查 str1 是否和 str2 相同
str1 != str2 | 检查 str1 是否和 str2 不同
str1 < str2 | 检查 str1 是否比 str2 小
str1 > str2 | 检查 str1 是否比 str2 大
-n str1 | 检查 str1 的长度是否非 0
-z str1 | 检查 str1 的长度是否为 0
-------------+----------------------------
例如,判断字符串是否为空:
a=hello
if [ -n $a ]
then
echo "'$a' is not empty"
else
echo "'$a' is empty"
fi
5.3 文件比较
test 命令的文件比较功能
----------------+--------------------------------------------
比 较 | 描 述
----------------+--------------------------------------------
-d file | 检查 file 是否存在并是一个目录
-e file | 检查 file 是否存在
-f file | 检查 file 是否存在并是一个文件
-r file | 检查 file 是否存在并可读
-s file | 检查 file 是否存在并非空
-w file | 检查 file 是否存在并可写
-x file | 检查 file 是否存在并可执行
-O file | 检查 file 是否存在并属当前用户所有
-G file | 检查 file 是否存在并且默认组与当前用户相同
file1 -nt file2 | 检查 file1 是否比 file2 新
file1 -ot file2 | 检查 file1 是否比 file2 旧
----------------+--------------------------------------------
例如,检查目录:
dir=/home/zhbi
if [ -d $dir ]
then
echo "$dir exists"
else
echo "$dir does not exist"
fi
6. if-then 高级特性
bash shell 提供了两项可在
if-then
语句中使用的高级特性:
- 用于数学表达式的双括号
(( expression ))
- 用于高级字符串处理功能的双方括号
[[ expression ]]
6.1. 双方括号用法
双括号命令符号:
双括号命令符号
--------+---------
符 号 | 描 述
--------+---------
val++ | 后增
val-- | 后减
++val | 先增
--val | 先减
! | 逻辑求反
~ | 位求反
** | 幂运算
<< | 左位移
>> | 右位移
& | 位布尔和
| | 位布尔或
&& | 逻辑和
|| | 逻辑或
--------+---------
双括号命令使用:
a=10
if (( $a ** 2 > 90 ))
then
(( b = $a ** 2 ))
echo "The square of $a is $b"
fi
6.2 使用双方括号
注意,不是所有的shell都支持双方括号。在下面的脚本中,我们使用了双等号( == )。双等号将右边的字符串( r* )视为一个模式,并应用模式匹配规则。双方括号命令 $usr环境变量进行匹配,看它是否以字母 r 开头。如果是的话,比较通过,shell会执行 then 部分的命令。
if [[ $usr == r* ]]
then
echo "Hello $user"
else
echo "Sorry, I do not know you"
fi
7. case 命令
s="word"
case $s in
"hello") echo This is hello;;
"word") echo This is word;;
"A" | "B") echo good;;
esac