if-then 语句的基本格式:
if command
then
commands
elif
commands
else
commands
fi
bash 的 if 语句会运行 if 后面的命令。如果命令的退出状态码是0(命令运行正确),则运行位于 then 部分的命令。如果是其他值,执行 else 部分的命令。
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
if ls dir
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
ls: 无法访问 'dir': 没有那个文件或目录
exec else commands
zzz@ubuntu:~/my_learning$ vim test.sh
zzz@ubuntu:~/my_learning$
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
if ls .
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
test.sh
exec then commands
zzz@ubuntu:~/my_learning$
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/bash
a="My_Name"
if test $a
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@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/bash
a=""
if test $a
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
exec else commands
zzz@ubuntu:~/my_learning$
这种方式,无需声明 test ,而是使用方括号,要注意左括号右边和右括号左边的空格,否则报错。基本格式如下:
if [ condition ]
then
commands
fi
可以判断的三类条件:
test 的数值比较只能处理整数,不能处理浮点数。
比较 | 描述 |
---|---|
-eq | 相等 |
-ge | 大于等于 |
-gt | 大于 |
-le | 小于等于 |
-lt | 小于 |
-ne | 不等于 |
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
a=4
if [ $a -eq 4 ]
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$
比较 | 描述 |
---|---|
= | 相等 |
!= | 不相等 |
< | 小于 |
> | 大于 |
-n str1 | 长度是否非0 |
-z str1 | 长度是否为0 |
注意
字符串比较时,需要注意字符串的两个顺序问题:
- 大于号和小于号必须使用转义字符 \,否则将被视为重定向符号;
- 大于小于顺序和sort命令采用的不同:sort 中大号字母被认为是大于小写字母的;
可以测试Linux文件系统上的文件和目录。
比较 | 描述 |
---|---|
-d file | 是否存在且是目录 |
-e file | 是否存在,可以是目录或文件 |
-f file | 是否存在且是文件 |
-r file | 文件是否存在并可读 |
-s file | 文件是否存在并非空 |
-w file | 文件是否存在并可写 |
-x file | 文件是否存在并可执行 |
-o file | 文件是否存在并属当前用户 |
-G file | 文件是否存在并且默认组与当前用户相同 |
file1 -nt file2 | 检查file1是否比file2新 |
file1 -ot file2 | 检查file1是否比file2旧 |
布尔运算 and , or 。
[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
bash shell 提供了两项可在 if-then 语句中使用的高级特性:
符号 | 描述 |
---|---|
val++, val–,++val, --val | 后增,后减,先增,先减 |
!, ~ | 逻辑求反,位求反 |
** | 幂运算 |
<<, >> | 左位移,右位移 |
&, | | 位布尔和,位布尔或 |
&&, || | 逻辑和,逻辑或 |
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
a=4
if (( $a **2 >10))
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$
注:双括号中的大于小于号不需要转义。
双括号中使用 test 测试中的标准字符串比较。另外,双方括号提供了一个特性——模式匹配,可以使用正则表达式。
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
a="my_name"
if [[ $a = *name ]]
then
echo "exec then commands"
else
echo "exec else commands"
fi
zzz@ubuntu:~/my_learning$ ./test.sh
exec then commands
zzz@ubuntu:~/my_learning$
case 命令采用列表格式来检查单个变量的多个值,执行不同的命令。可以通过竖线操作在一行中分隔多个模式。*会捕获所有与已知模式不匹配的值。
基本格式:
case variable in
pattern1) commands;;
pattern2 | pattern3) commands;;
*) default commands;;
esac
zzz@ubuntu:~/my_learning$ cat test.sh
#!/bin/bash
a="s2"
case $a in
s1 | s2)
echo "exec s1 or s2 commands";;
s3)
echo "exec s3 commands";;
*)
echo "exec * commands";;
esac
zzz@ubuntu:~/my_learning$ ./test.sh
exec s1 or s2 commands
zzz@ubuntu:~/my_learning$