shell 编程(四)、条件测试
测试语句
一、条件测试语法
在bash的各种流程结构中通常要进行各种测试,然后根据测试结果执行不同的操作,有时也会通过与if等条件语句结合,使我们可以方便的完成判断。
格式1:test 测试表达式
等价于 格式2:【测试表达式】 //作者习惯使用
格式3:【【测试表达式】】 //扩展使用,网友推荐使用
注意:
1、在[[]]中可以使用通配符进行模式匹配;
2、&&、||、>、<等操作符可以运用在[[]]中,但不能用在[]中;
3、对于整数进行关系运算,也可以使用shell的算术运算符(())
[centos@localhost ~]$ test -f file && echo true || echo false
false
[centos@localhost ~]$ touch file
[centos@localhost ~]$ test -f file && echo true || echo false
true
[centos@localhost ~]$ test ! -f file && echo true || echo false //非
false
[centos@localhost ~]$ rm -rf file
[centos@localhost ~]$ [ -f file ] && echo true || echo false
false
[centos@localhost ~]$ [ ! -f file ] && echo true || echo false
true
[centos@localhost ~]$ rm -rf file
[centos@localhost ~]$ [[ -f file ]] && echo true || echo false
false
[centos@localhost ~]$ [[ ! -f file ]] && echo true || echo false
true
[centos@localhost ~]$ [[ -f file && -f folder ]] && echo true || echo false //注意
false
[centos@localhost ~]$ [ -f file && -f folder ] && echo true || echo false
-bash: [: missing `]'
false
[centos@localhost ~]$ [ -f file -a -f folder ] && echo true || echo false
false
二、文件测试操作符
三、字符串测试操作符
字符串操作符的作用:比较两个字符串是否相同、字符串长度是否为零,字符串是否为NULL
"=" 等价于"==" 比较两个字符串是否相同,eg: if[ "$a" = "$b" ] ,最好用 if[ "${a}" = "${b}" ]
"!=" 比较两个字符串是否相同,不同则为“是”
四、整数二元比较操作符
[centos@localhost ~]$ [ 2 < 1 ] && echo 1 || echo 0 //不转义,错误,不报错!
1
[centos@localhost ~]$ [ 2 \> 1 ] && echo 1 || echo 0
1
[centos@localhost ~]$ [ 2 \< 1 ] && echo 1 || echo 0
0
[centos@localhost ~]$ [ 2 -gt 1 ] && echo 1 || echo 0 //正常
1
[centos@localhost ~]$ [ 2 -lt 1 ] && echo 1 || echo 0
0
[centos@localhost ~]$ [[ 2 > 1 ]] && echo 1 || echo 0
1
[centos@localhost ~]$ [[ 2 < 1 ]] && echo 1 || echo 0
0
[centos@localhost ~]$ [[ 2 -gt 1 ]] && echo 1 || echo 0 //也可以
1
[centos@localhost ~]$ [[ 2 -lt 1 ]] && echo 1 || echo 0
0
//二元字符比较
[centos@localhost ~]$ [ "a" > "bc" ] && echo 1 || echo 0
1
[centos@localhost ~]$ [ "a" \> "bc" ] && echo 1 || echo 0 //需要转义
0
[centos@localhost ~]$ [ "a" < "bc" ] && echo 1 || echo 0
1
[centos@localhost ~]$ [ "a" \< "bc" ] && echo 1 || echo 0
1
五、逻辑操作符
以此举例如下:
条件测试举例:
[centos@localhost ~]$ [ -f "$file" ] && echo true || echo false //条件表达式的用法,变量$file 加了双引号是好习惯防止发生意外错误!
false
[centos@localhost ~]$ if [ -f "$file" ]; then echo true;else echo false;fi //if 语句的用法
false
文件测试举例:
[centos@localhost ~]$ file1=/etc/services
[centos@localhost ~]$ file2=/etc/rc.local
[centos@localhost ~]$ file3=/etc/abc
[centos@localhost ~]$ echo $file1 $file2 file3
/etc/services /etc/rc.local file3
[centos@localhost ~]$ [ -f "$file1" ] && echo true || echo false
true
[centos@localhost ~]$ [ -f "$file3" ] && echo true || echo false
false
[centos@localhost ~]$ [ -d "$file1" ] && echo true || echo false
false
[centos@localhost ~]$ [ -s "$file1" ] && echo true || echo false
true
[centos@localhost ~]$ [ -e "$file1" ] && echo true || echo false
true
[centos@localhost ~]$ [ -e "$file3" ] && echo true || echo false
false
[centos@localhost ~]$ ll /etc/abc
ls: 无法访问/etc/abc: 没有那个文件或目录
[centos@localhost ~]$ [ -e /etc/ ] && echo true || echo false //加引号与否无所谓
true
[centos@localhost ~]$ echo $file7 //注意:如果变量不加“”,结果可能就不正确!
[centos@localhost ~]$ [ -f $file7 ] && echo true || echo false
true
[centos@localhost ~]$ [ -f "$file7" ] && echo true || echo false
false
[centos@localhost ~]$ more /etc/init.d/nfs //多学习系统脚本!
#!/bin/sh
#
# nfs This shell script takes care of starting and stopping
# the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file sharing across networks.
# This service provides NFS server functionality, which is \
# configured via the /etc/exports file.
# probe: true
# config: /etc/sysconfig/nfs
### BEGIN INIT INFO
# Provides: nfs
# Required-Start: $local_fs $network $syslog $rpcbind
# Required-Stop: $local_fs $network $syslog $rpcbind
# Default-Stop: 0 1 6
# Short-Description: Start up the NFS server sevice
# Description: NFS is a popular protocol for file sharing across networks \
# This service provides NFS server functionality, \
# which is configured via the /etc/exports file.
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network //存在则加载,把文件里的变量值带到该启动脚本里面来。
# Check for and source configuration file otherwise set defaults
[ -f /etc/sysconfig/nfs ] && . /etc/sysconfig/nfs
多条件文件测试:
可以 与(-a &&) 或(-o ||) 非(!)将条件表达式连接起来,接着上面的变量。
[centos@localhost ~]$ echo $file1 $file2 file3
/etc/services /etc/rc.local file3
[centos@localhost ~]$ [ -f "$file1" -o -e "$file2" ] && echo true || echo false
true
[centos@localhost ~]$ [ -f "$file1" -a -e "$file2" ] && echo true || echo false
true
[centos@localhost ~]$ [ -f "$file1" -a -e "$file3" ] && echo true || echo false
false
[centos@localhost ~]$ echo $file3
/etc/abc
[centos@localhost ~]$ ll /etc/abc
ls: 无法访问/etc/abc: 没有那个文件或目录
注意:
[centos@localhost ~]$ [ -f "$file1" && -e "$file3" ] && echo true || echo false //-a 和 -o用在[]中
-bash: [: missing `]'
false
[centos@localhost ~]$ [[ -f "$file1" && -e "$file3" ]] && echo true || echo false //&& || 用在[[ ]]中
false
//系统bind启动脚本举例:named
[centos@localhost ~]$ [ -r /etc/sysconfig/network ] && . /etc/sysconfig/network //如果(条件成立),就。
[centos@localhost ~]$ [ -x /usr/sbin/$named ] || exit 5 //如果不(条件不成立),就。
[centos@localhost ~]$ cat > test.sh //可以使用条件表达式
[ 3 -ne 3 ]|| {
echo "oldboy..."
echo "coming..."
exit 1
}
[centos@localhost ~]$ sh test.sh
oldboy...
coming...
[centos@localhost ~]$
字符串测试举例:
[centos@localhost ~]$ file1=/etc/services
[centos@localhost ~]$ echo $file1
/etc/services
[centos@localhost ~]$ [ -n "$file1" ] && echo true || echo false
true
[centos@localhost ~]$ [ -z "$file1" ] && echo true || echo false
false
[centos@localhost ~]$ more /etc/init.d/nfs //脚本查看
#!/bin/sh
#
# nfs This shell script takes care of starting and stopping
# the NFS services.
多条件字符串测试:
[centos@localhost ~]$ echo $file1 $file2
/etc/services
[centos@localhost ~]$ [ -z "$file1" -o -z "$file2" ] && echo true || echo false
true
[centos@localhost ~]$ [ -z "$file1" -a -z "$file2" ] && echo true || echo false
false
[centos@localhost ~]$ [[ -z "$file1" -o -z "$file2" ]] && echo true || echo false
bash: syntax error in conditional expression
bash: syntax error near `-o'
[centos@localhost ~]$ [[ -z "$file1" || -z "$file2" ]] && echo true || echo false
true
[centos@localhost ~]$ [[ -z "$file1" && -z "$file2" ]] && echo true || echo false
false
[centos@localhost ~]$ echo $file1 $file2
/etc/services /etc/rc.local
[centos@localhost ~]$ [ "$file1" = "$file2" ] && echo true || echo false
false
[centos@localhost ~]$ [ "$file1" == "$file2" ] && echo true || echo false
false
[centos@localhost ~]$ [ "$file1" != "$file2" ] && echo true || echo false
true
[centos@localhost ~]$
[centos@localhost ~]$ [ "$file1" > "$file2" ] && echo true || echo false
bash: /etc/rc.local: 权限不够
false
[centos@localhost ~]$ [ "$file1" < "$file2" ] && echo true || echo false
true
[centos@localhost ~]$ [ "$file1" \< "$file2" ] && echo true || echo false //不推荐,单[]需要转义
false
[centos@localhost ~]$ [[ "$file1" < "$file2" ]] && echo true || echo false //双[]可以的
false
[centos@localhost ~]$ [[ "${#file1}" < "${#file2}" ]] && echo true || echo false
false
[centos@localhost ~]$ echo ${#file1}
13
[centos@localhost ~]$ echo ${#file2}
13
整数测试举例:
[centos@localhost ~]$ a1=10;a2=13
[centos@localhost ~]$ echo a1 a2
a1 a2
[centos@localhost ~]$ [ $a1 -eq $a2 ] && echo true || echo false
false
[centos@localhost ~]$ [ $a1 -gt $a2 ] && echo true || echo false
false
[centos@localhost ~]$ [ $a1 -lt $a2 ] && echo true || echo false
true
[centos@localhost ~]$ [ $a1 -ne $a2 ] && echo true || echo false
true
[centos@localhost ~]$ a=0001
[centos@localhost ~]$ b=100
[centos@localhost ~]$ [ $a -ge $b ] && echo true || echo false
false
[centos@localhost ~]$ [ "$a" -ge "$b" ] && echo true || echo false //还是当成整数看
false
test命令测试的用法://等价于上面的用法。
[centos@localhost ~]$ echo $file1 $file2
/etc/services /etc/rc.local
[centos@localhost ~]$ test -z "$file1" && echo true || echo false
false
[centos@localhost ~]$ test 1 -eq 2 && echo true || echo false
false
//&& 和 -a与逻辑符
[centos@localhost ~]$ cat >test1.sh
echo -n "please input :"
read m n
if [ ${m} -eq 1 ] && [ ${n} -eq 2 ];then 等价
//if [ ${m} -eq 1 -a ${n} -eq 2 ];then 等价
//if [[ ${m} -eq 1 && ${n} -eq 2 ]];then 等价
echo "good"
else echo "bad"
fi
[centos@localhost ~]$ sh test1.sh
please input :2 4
bad
[centos@localhost ~]$ sh test1.sh
please input :4 2
bad
[centos@localhost ~]$ sh test1.sh
please input :1 2
good