1.条件测试的基本语法
在shell程序中,用户可以使用测试语句来测试指定的条件表达式的条件的真或假。当指定的条件为真时,整个条件测试的返回值为0;反之,如果指定的条件为假,则条件测试语句的返回值为非0值。
条件测试语法 | 说明 |
---|---|
语法1:test <测试表达式> | test命令和<测试表达式>之间至少有一个空格 |
语法2:[<测试表达式>] | 该方法和test命令的用法一样,[]的边界和内容之间至少有一个空格 |
语法3:[[<测试表达式>]] | 比test和[]更新的语法格式。[[]]的边界和内容之间至少有一个空格。[[]]中可以使用通配符等进行模式匹配。 |
语法4: ((<测试表达式>)) | 一般用于if语句里,双小括号两端不需要空格,测试对象只能是整数 |
2.文件测试表达式
常用的文件测试操作符 | 说明 |
---|---|
-a/-e 文件 | 文件是否存在 |
-b 文件 | 文件是否存在,且为块文件,如果文件存在且是一个块文件,则结果0 |
-c 文件 | 文件是否存在且为字符文件,如果文件存在且是一个字符文件,则结果为0 |
-L 文件 | 文件存在且为链接文件则为真 |
-d 文件 | 文件存在且为目录则为真,即测试表达式成立 |
-f 文件 | 文件存在且为普通文件则为真,即测试表达式成立 |
-s 文件 | 文件存在且文件大小部位0则为真 |
-u 文件 | 文件是否设置suid位,如果设置了suid,则结果为0 |
-r 文件 | 文件存在且可读为真 |
-w 文件 | 文件存在且可写为真 |
-x 文件 | 文件存在且可执行则为真 |
f1 -nt f2,nt为newer than | 文件f1比f2新则为真,根据文件的修改时间来计算 |
f1 -ot f2,ot为older than | 文件f1比文件f2旧则为真,根据文件的修改时间来计算 |
测试文件的读、写、执行等属性,不光是根据文件属性rwx的标识来判断,还要看当前执行测试的用户是否真的可以按照对应的权限操作文件。
1>test示例:
[root@centos-7 day3]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 17 02:05 file
[root@centos-7 day3]# test -f file;echo $? #判断是否存在file这样一个文件,存在则输出0
0
[root@centos-7 day3]# test -f file1;echo $?
1
[root@centos-7 day3]# test -x file;echo $? #判断file文件是否有执行的权限,有则输出0
1
2>[]示例(注意测试表达式的方括号两边需要有空格)
[root@centos-7 day3]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 17 02:05 file
[root@centos-7 day3]# [ -f file ];echo $?
0
[root@centos-7 day3]# [ -f file1 ];echo $?
1
[root@centos-7 day3]# [ -w file ];echo $?
0
3> [[]]示例(注意测试表达式和[[]]两边需要有空格)
[root@centos-7 day3]# ll
总用量 0
-rw-r--r--. 1 root root 0 8月 17 02:05 file
[root@centos-7 day3]# [[ -f file ]];echo $?
0
[root@centos-7 day3]# [[ -f file1 ]];echo $?
1
[root@centos-7 day3]# [[ -x file ]];echo $?
1
注意:如果测试的文件路径是用变量来代替,变量一定要加引号
[root@centos-7 day3]# echo $filepath
[root@centos-7 day3]# test -f $filepath;echo $?
0
[root@centos-7 day3]# test -f "$filepath";echo $?
1
练习1:让用户输入一个文件名,并做如下判断:
(1)如果用户输入的文件为空时显示:you must input a filename,并中断程序;
(2)如果用户输入的文件不存在时,显示the file do not exist , 并中断程序;
(3)如果文件存在,判断该文件的文件类型和执行者对该文件所拥有的权限。
说明:由于root用户在很多权限的限制上面都是无效的,所以使用root执行这个脚本时,常常会发现与ls -l 的结果不相同。所以建议使用一般用户来执行这个脚本。
[root@centos-7 day3]# vim test.sh
#!/bin/bash
read -p "input a filename:" filename
test -z $filename && echo "you must input a filename" && exit 0
test ! -e $filename && echo "the file $filename do not exist" && exit 0
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
echo "the $filename is a $filename"
echo "and the permissons are: $perm"
[root@centos-7 day3]# chmod a+rx test.sh
[root@centos-7 day3]# ll
总用量 4
-rw-r--r--. 1 root root 0 8月 17 02:05 file
-rwxr-xr-x. 1 root root 626 8月 17 02:28 test.sh
[root@centos-7 day3]# ./test.sh
input a filename:file1
the file file1 do not exist
[root@centos-7 day3]# ./test.sh
input a filename:file
the file is a file
and the permissons are: readable writable
3.字符串测试表达式
常用字符串测试操作符 | 说明 |
---|---|
-n “字符串” | 若字符串的长度不为0,则为真,即测试表达式成立,n可以理解为no zero |
-z “字符串” | 若字符串的长度为0,则为真,z可以理解为zero |
“串1”=“串2” | 若字符串1等于字符串2,则为真可以使用==代替= |
“串1”!=“串2” | 若字符串1不等于字符串2.则为真 |
1>test示例
[root@centos-7 day3]# test -n abc;echo $?
0
[root@centos-7 day3]# test -n "";echo $?
1
[root@centos-7 day3]# test -n " ";echo $?
0
[root@centos-7 day3]# test -z '';echo $?
0
[root@centos-7 day3]# test -z abc;echo $?
1
[root@centos-7 day3]# test -z ' ';echo $?
1
[root@centos-7 day3]# test abc=abcd ;echo $?
0
2>[]示例
[root@centos-7 day3]# [ -n '' ];echo $?
1
[root@centos-7 day3]# [ -n ' ' ];echo $?
0
[root@centos-7 day3]# [ -z '' ];echo $?
0
[root@centos-7 day3]# [ abc=abcd ];echo $?
0
[root@centos-7 day3]# [ abc = abcd ];echo $?
1
3>[[]]示例
[root@centos-7 day3]# [[ -n abc ]];echo $?
0
[root@centos-7 day3]# [[ -n ' ' ]];echo $?
0
[root@centos-7 day3]# [[ -n '' ]];echo $?
1
[root@centos-7 day3]# [[ abc=acd ]];echo $?
0
[root@centos-7 day3]# [[ abc = acd ]];echo $?
1
注意:测试对象是变量时,变量需要加引号
[root@centos-7 day3]# test -n $name;echo $?
0
[root@centos-7 day3]# test -n "$name";echo $?
1
[root@centos-7 day3]# [ -n $name ];echo $?
0
[root@centos-7 day3]# [ -n "$name" ];echo $?
1
4.整数测试表达式
在[]以及test中使用的比较符号 | 在(())和[[]]中使用的比较符号 | 说明 |
---|---|---|
-eq | ==或= | 相等全拼为equal |
-ne | != | 不相等,全拼为not equal |
-gt | > | 大于,全拼greater than |
-ge | >= | 大于等于,greater equal |
-lt | < | 小于,less than |
-le | <= | 小于等于,less equal |
注意:
=和!=也可在[]中作比较时使用,在[]中也可使用>和<符号,但需要使用反斜线转义,有时不转译虽然语法不会报错,但是结果可能会不对;
在[[]]中也可使用包含-gt和-lt的符号,不建议使用;
比较符号两端也要有空格。
1>test示例
[root@centos-7 day3]# test 2 -eq 3;echo $?
1
[root@centos-7 day3]# test 2 -eq 2;echo $?
0
2>[]示例
[root@centos-7 day3]# [ 2 -ne 3 ];echo $?
0
[root@centos-7 day3]# [ 2 -ne 2 ];echo $?
1
3>[[]]示例
[root@centos-7 day3]# [[ 2 != 3 ]];echo $?
0
[root@centos-7 day3]# [[ 2 != 2 ]];echo $?
1
[root@centos-7 day3]# [[ 2!=2 ]];echo $? 未写空格,导致出错
0
4>(())示例
[root@centos-7 day3]# (( 2!=3 ));echo $?
0
[root@centos-7 day3]# ((2!=3));echo $?
0
[root@centos-7 day3]# ((2=3));echo $?
-bash: ((: 2=3: 尝试给非变量赋值 (错误符号是 "=3")
1
[root@centos-7 day3]# ((2==3));echo $?
1
[root@centos-7 day3]# ((2>3));echo $?
1
[root@centos-7 day3]# ((2<3));echo $?
0
5.逻辑操作符
在[]使用逻辑操作符 | 在test、[[]]和(())中使用的逻辑操作符 | 说明 |
---|---|---|
-a | && | and,与,两端都为真,则结果为真 |
-o | or,或,两端有一个为真,则结果为真 | |
! | ! | not,非,两端相反,则结果为真 |
1>test示例
[root@centos-7 day3]# ll
总用量 0
drwxr-xr-x. 2 root root 6 8月 17 04:22 ceshi
-rw-r--r--. 1 root root 0 8月 17 04:22 file
[root@centos-7 day3]# test -f file && echo 1 ||echo 0
1
[root@centos-7 day3]# test -f file1 && echo 1 ||echo 0
0
[root@centos-7 day3]# ! test -f file;echo $?
1
2>[]示例
[root@centos-7 day3]# [ -f ceshi -a -f file ];echo $?
1
[root@centos-7 day3]# [ -e ceshi -a -f file ];echo $?
0
[root@centos-7 day3]# [ -f ceshi -o -f file ];echo $?
0
[root@centos-7 day3]# [ -f ceshi ];echo $?
1
[root@centos-7 day3]# ! [ -f ceshi ];echo $?
0
[root@centos-7 day3]# [ ! -f ceshi ];echo $?
0
使用&&等符号的错误示例:
[root@centos-7 day3]# [ -f ceshi && -f file ];echo $?
-bash: [: 缺少 `]'
2
[root@centos-7 day3]# [ -f ceshi || -f file ];echo $?
-bash: [: 缺少 `]'
-bash: -f: 未找到命令
127
使用&&等符号的正确示例:
[root@centos-7 day3]# [ -f file ] && [ -f ceshi ];echo $?
1
[root@centos-7 day3]# [ -f file ] || [ -f ceshi ];echo $?0
00
[root@centos-7 day3]# [ -f file ] || [ -d ceshi ];echo $?
0
3>[[]]示例
[root@centos-7 day3]# [[ -f file && -f ceshi ]];echo $?
1
[root@centos-7 day3]# [[ -f file || -f ceshi ]];echo $?
0
[root@centos-7 day3]# [[ -f file && -d ceshi ]];echo $?
0
4>(())示例
[root@centos-7 day3]# ((2>3&&3>4));echo $?
1
[root@centos-7 day3]# ((2<3&&3<4));echo $?
0
实验1:通过read传入一个数字,如果传入的数字等于1,就打印1;如果等于2,就打印2,如果不等于1也不等于2,就提示输入不对,然后退出程序。
[root@centos-7 day3]# vim 1.sh
read -p "please input a number:" num
[ "$num" -eq 1 ] && {
echo 1
exit 0
}
[ "$num" -eq 2 ] && {
echo 2
exit 0
}
[ "$num" -ne 1 -a "$num" -ne 2 ] && {
echo error
exit 0
}
[root@centos-7 day3]# ./1.sh
please input a number:1
1
[root@centos-7 day3]# ./1.sh
please input a number:22
error
实验2:通过read读入两个整数,并比较他们的大小。
[root@centos-7 day3]# vim 2.sh
read -p "please input two number:" a b
[ -z "$a" -o -z "$b" ] && {
echo "please input 'two' number"
exit 1
}
expr $a + 10 &>/dev/null
return_a=$?
expr $b + 10 &>/dev/null
return_b=$?
[ "$return_a" -eq 0 -a "$return_b" -eq 0 ] || {
echo "please input two 'number'"
exit 2
}
[ "$a" -lt "$b" ] && {
echo "$a < $b"
exit 0
}
[ "$a" -eq "$b" ] && {
echo "$a = $b"
exit 0
}
[ "$a" -gt "$b" ] && {
echo "$a > $b"
exit 0
}
[root@centos-7 day3]# chmod a+rx 2.sh
[root@centos-7 day3]# ./2.sh
please input two number:2 3
2 < 3
[root@centos-7 day3]# ./2.sh
please input two number:6 6
6 = 6
实验3:假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据:程序的文件名;共有几个参数;若参数的个数小于2个则告知用户参数数量太少;全部的参数内容;第一个参数;第二个参数。
echo "the script name is $0"
echo "the parameter number is $#"
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2." && exit 0
在方括号内的每个组件都需要由空格键来分隔(特别注意中括号的两端需要有空格符来分隔);在方括号内的变量,最好都要以双引号括起;在方括号内的常量最好都以单或双引号括起来。(中括号的使用方法与test几乎一模一样)
echo "your whole parameter is '$@'"
echo "the 1st parameter is $1"
echo "the 2nd parameter is $2"
特殊条件测试表达式案例:
[ 条件1 ] && {
命令1
命令2
命令3
}
[[ 条件1 ]] && {
命令1
命令2
命令3
}
test 条件1 && {
命令1
命令2
命令3
}
if [ 条件1 ]
then
命令1
命令2
命令3
fi