条件测试

条件测试语句

判断字符串是否相等, 可能还要检查文件状态或进行数字测试, 只有这些测试完成才能做下一步动作

test 命令: 用于测试字符串文件状态数字

test 命令有两种格式:

test condition

使用方括号时, 要注意在条件两边加上空格

[ condition ]

shell 脚本中的条件测试如下:文件测试字符串测试数字测试复合测试

文件

文件测试: 测试文件状态的条件表达式

-e 是否存在

-d 是目录

-f 是文件

-r 可读

-w 可写

-x 可执行

-L 符号连接

-c 是否字符设备

-b 是否块设备

-s 文件非空

栗子 : test_file.sh

#!/bin/bash

test -e /dev/qaz
echo $?

test -e /home
echo $?

test -d /home
echo $?

test -f /home
echo $?

mkdir test_sh

chmod 500 test_sh
[ -r test_sh ]
echo $?

[ -w test_sh ]
echo $?

[ -x test_sh ]
echo $?

[ -s test_sh ]
echo $?

[ -c /dev/console ]
echo $?

[ -b /dev/sda ]
echo $?

[ -L /dev/stdin ]
echo $?

字符串

字符串测试

test str_operator “str”

test “str1” str_operator “str2”

[ str_operator “str” ]
[ “str1” str_operator “str2” ]

其中 str_operator 可以是:

= 两个字符串相等

!= 两个字符串不相等

-z 空串

-n 非空串

栗子 : test_string.sh

#!/bin/bash

test -z $yn
echo $?

echo "please input a y/n"

read yn

[ -z "$yn" ]
echo 1:$?

[ $yn = "y" ]
echo 2:$?

数字

测试数值格式如下:

test num1 num_operator num2
[ num1 num_operator num2 ]

num_operator 可以是 :

数值相等

-eq

数值不相等

-ne

数 1 大于数 2

-gt

数 1 大于等于数 2

-ge

数 1 小于等于数 2

-le

数 1 小于数 2

-lt

栗子 : test_num.sh

#!/bin/bash

echo "please input a num(1-9)"
read num

[ $num -eq 5 ]
echo $?

[ $num -ne 5 ]
echo $?

[ $num -gt 5 ]
echo $?

[ $num -ge 5 ]
echo $?

[ $num -le 5 ]
echo $?

[ $num -lt 5 ]
echo $?

复合测试

命令执行控制:

&&

command1 && command2

&&左边命令(command1) 执行成功( 即返回 0 ), shell 才执行 && 右边的命令(command2)

||

command1 || command2

||左边的命令(command1) 执行失败 ( 即返回非 0 ), shell 才执行 || 右边的命令(command2)

栗子 :

test -e /home && test -d /home && echo "true"

test 2 -lt 3 && test 5 -gt 3 && echo "equal"

test "aaa" = "aaa" || echo "not equal" && echo "equal"

多重条件判定

-a

( and )两状况同时成立! 同时具有 r 与 x 权限时, 才为 true.

test -r file -a -x file file

-o

(or)两状况任何一个成立! 具有 r 或 x 权限时, 就传回 true.

test -r file -o -x file file

!

相反状态 , 当 file 不具有 x 时, 回传 true

test ! -x file

Last updated