3. shell语法——test命令与判断符号[]

逻辑运算符&&和||

  • && 表示与,|| 表示或
  • 二者具有短路原则:
    expr1 && expr2:当expr1为假时,直接忽略expr2
    expr1 || expr2:当expr1为真时,直接忽略expr2
  • 表达式的exit code为0,表示真;为非零,表示假。(C/C++中的定义相反

test命令
在命令行中输入man test,可以查看test命令的用法。

test命令用于判断文件类型,以及对变量做比较。

test命令用exit code返回结果,而不是使用stdout。0表示真,非0表示假。

例如:

1
2
3
4
5
6
7
8
test 2 -lt 3  # 为真,返回值为0
echo $? # 输出上个命令的返回值,输出0
acs@9e0ebfcd82d7:~$ ls # 列出当前目录下的所有文件
homework output.txt test.sh tmp
acs@9e0ebfcd82d7:~$ test -e test.sh && echo "exist" || echo "Not exist"
exist # test.sh 文件存在
acs@9e0ebfcd82d7:~$ test -e test2.sh && echo "exist" || echo "Not exist"
Not exist # testh2.sh 文件不存在

文件类型判断
命令格式:

1
test -e filename  # 判断文件是否存在
测试参数 代表意义
-e 文件是否存在
-f 是否为文件
-d 是否为目录

文件权限判断
命令格式:

1
test -r filename  # 判断文件是否可读
测试参数 代表意义
-r 文件是否可读
-w 文件是否可写
-x 文件是否可执行
-s 是否为非空文件

字符串比较

测试参数 代表意义
test -z STRING 判断STRING是否为空,如果为空,则返回true
test -n STRING 判断STRING是否非空,如果非空,则返回true(-n可以省略)
test str1 == str2 判断str1是否等于str2
test str1 != str2 判断str1是否不等于str2

多重条件判定
命令格式:

1
test -r filename -a -x filename
测试参数 代表意义
-a 两条件是否同时成立
-o 两条件是否至少一个成立
! 取反。如 test ! -x file,当file不可执行时,返回true

判断符号[]

[]test用法几乎一模一样,更常用于if语句中。另外[[]][]的加强版,支持的特性更多。

例如:

1
2
3
4
5
6
7
8
[ 2 -lt 3 ]  # 为真,返回值为0
echo $? # 输出上个命令的返回值,输出0
acs@9e0ebfcd82d7:~$ ls # 列出当前目录下的所有文件
homework output.txt test.sh tmp
acs@9e0ebfcd82d7:~$ [ -e test.sh ] && echo "exist" || echo "Not exist"
exist # test.sh 文件存在
acs@9e0ebfcd82d7:~$ [ -e test2.sh ] && echo "exist" || echo "Not exist"
Not exist # testh2.sh 文件不存在

注意:

  • []内的每一项都要用空格隔开
  • 中括号内的变量,最好用双引号括起来
  • 中括号内的常数,最好用单或双引号括起来

例如:

1
2
3
4
5
name="acwing yxc"
[ $name == "acwing yxc" ] # 错误,等价于 [ acwing yxc == "acwing yxc" ],参数太多
[ "$name" == "acwing yxc" ] # 正确