1、测试文件状态
test命令一般有两种格式
test condition或
[condition]
注意:使用"[]"时,要在条件和"[]"之间加上空格测试文件状态的条件表达式很多,最常用的有以下几个
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
1、
例:
#!/bin/sh
#测试test.txt文件的读权限,使用"[]"方法
[ -r test.txt ]
echo $?
#测试test.txt文件的写权限,使用"[]"的方法
[ -w test.txt ]
echo $?
#测试test.txt文件的执行权限,使用"[]"的方法
[ -x test.txt ]
echo $?
#测试test.txt文件的读权限,使用"test命令"的方法
test -r test.txt
echo $?
#测试test.txt文件的写权限,使用"test命令"的方法
test -w test.txt
echo $?
#测试test.txt文件的执行权限,使用"test命令"的方法
test -x test.txt
echo $?
exit 0
2、使用ls命令查看test.txt的权限信息$ls -l test.txt
-rwxrw-r-- 1 linux-c linux-c 0 5 月 27 13:07 test.txt
3、在shell中运行该shell脚本如下
0
0
0
0
0
0
运行结果说明test.txt文件只有可读可写执行的权限。
2、测试时使用逻辑操作符与c语言类似,shell脚本中同样提供三种逻辑操作完成此功能。逻辑运算符通常和分支语句配合使用,实现程序执行流程的不同
1、-a逻辑与,两个操作数均为真,结果为真,否则为假。
2、-o逻辑或,两个操作数一个为真,结果为真,否则为假。
3、!逻辑非,条件为假,结果为真,否则为假。
例:
#!/bin/sh
[ -r test.txt -a -r test2.txt ]
echo $?
[ -w test.txt -o -w test2.txt ]
echo $?
[ ! -x test2.txt ]
echo $?
$ ls -l
---------- 1 hjx hjx 0 9月 11 11:40 test2.txt
-rwxrwxrwx 1 hjx hjx 120 9月 11 11:54 test.sh
-rwxrw-r-- 1 hjx hjx 0 9月 11 11:32 test.txt
$ ./test.sh
1
0
0
逻辑运算符
以下介绍 Shell的逻辑运算符,假定变量 a 为 10,变量 b 为 20:
运算符说明举例
&& 逻辑的 AND[[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 逻辑的 OR[[ $a -lt 100 || $b -gt 100 ]] 返回 true
#!/bin/sh
a=10
b=20
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "返回 true"
else
echo "返回 false"
fi
if [[ $a -lt 100 || $b -gt 100 ]]
then
echo "返回 true"
else
echo "返回 false"
fi
./test.sh
返回 false
返回 true
3、字符串测试
字符串测试有下5种格式
test "string"
test operator "string"
test "string1" operator "string2"
[operator "string"]
["string1" operator "string2"]
其中string1和string2代表需要测试的字符串,而operator代表字符串测试的操作符
这个操作共有以下4种可能的情况
1、=两个字符串相等
2、!= 两个字符串不等
3、-z 字符串是空串。
4、-n字符串是非空串。
例:
#!/bin/sh
str1="hello"
str2="hello"
str3= #str3是一个空串
#测试字符串str1和str2相等
test $str1=$str2
echo $?
#测试字符串str3是空串
test -z ${str3}
echo $?
#测试字符串str2是非空串
test -n $str2
echo $?
echo "using []" #使用"[]进行测试数据测试
#测试字符串str1和str2相等
[ $str1=$str2 ]
echo $?
#测试字符串str3是空串
[ -z $str3 ]
echo $?
#测试字符串str2是非空串
[ -n $str2 ]
echo $?
#测试字符串str1和str2不相等
[ $str1 != $str2 ]
echo $?
exit 0
$ ./test.sh
0
0
0
using []
0
0
0
1
4、测试数值test "number" operator "number"
或者
["number" operator "number"]
其中number代表要测试的数据,而operater代表测试的操作符
注:数据两边的双引号是不能省略的。这代表将数据化为字符串进行测试
operator有6种情况,如下所示
1、-ep两个数值相等
2、-ne两个数值不相等
3、-gt第一个数大于第二个数
4、-lt第一个数小于第二个数
5、-le第一个数小于等于第二个数
6、-ge第一个数大于等于第二个数
例:
#!/bin/sh
test "1" -eq "2"
echo $?
test "1" -ne "2"
echo $?
test "1" -gt "2"
echo $?
test "1" -ge "2"
echo $?
test "1" -lt "2"
echo $?
test "1" -le "2"
echo $?
echo
[ "1" -eq "2" ]
echo $?
[ "1" -ne "2" ]
echo $?
[ "1" -gt "2" ]
echo $?
[ "1" -ge "2" ]
echo $?
[ "1" -lt "2" ]
echo $?
[ "1" -le "2" ]
echo $?
例:
#!/bin/sh
num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo '两个数相等!'
else
echo '两个数不相等!'
fi
例:
num1="ru1noob"
num2="runoob"
if test $num1 = $num2
then
echo '两个字符串相等!'
else
echo '两个字符串不相等!'
fi
例:
#!/bin/sh
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi