shell-逻辑操作符讲解与文件条件测试多范例多生产案例

1. 逻辑操作符
  在书写测试表达式时,可以使用表1.1中的逻辑操作符实现复杂的条件测试
  表1.1逻辑连接符

提示:
  !   中文意思是反:与一个逻辑值相反的逻辑值
  -a   中文意思是与(and &&):两个逻辑值都为“真”,返回值才为“真”,反之为“假”
  -o   中文意思是或(or ||):两个逻辑值只要有一个为“真” ,返回值就为“真”

  逻辑操作符运算规则

结论:-a &&的运算规则:只有两端都是1才为真
真 1 假0
---------------------
and 1*0=0 假
and 1*1=1 真
and 0*0=0 假
两端都是1才为真

结论:-o,|| 两端都是0才为假,任何一端不为0都是真
---------------------
or 1+0=1 真
or 1+1=2 真
or 0+1=1 真
or 0+0=0 假
两端都是0才为假,不为0就是真。

2. 条件测试举例
   下面举一些条件测试的例子。为了清晰的显示测试结果,我们在屏幕上输出1(表示真)或0(表示假)表示测试结果。
   提示:这和前面的状态变量"$?"返回0为真不一样哟

[root@test-1 scripts]# [ -f "$file1" ] && echo 1 || echo 0     #这是条件表达式的用法
0
[root@test-1 scripts]# if [ -f "$file1" ];then echo 1;else echo 0;fi   #这是后面要讲的if条件句的语法。
0

  提示:
  1. 以上两条语句的功能是等同的。
  2. 变量$file加了双引号,这是编程的好习惯,可以防止很多意外的错误发生。
3. 文件测试举例
   首先我们定义file1和file2两个变量,并分别赋予两个系统文件路径及文件名的值。

[root@test-1 scripts]# file1=/etc/services;file2=/etc/rc.local 
[root@test-1 scripts]# echo $file1 $file2
/etc/services /etc/rc.local

  范例1:对单个文件的测试:

[root@test-1 scripts]# [ -f "$file1" ] && echo 1 || echo 0   #文件存在且为普通文件所以为真(1)
1
[root@test-1 scripts]# [ -d "$file1" ] && echo 1 || echo 0   #是文件不是目录所以为假(0)
0
[root@test-1 scripts]# [ -s "$file1" ] && echo 1 || echo 0    #文件存在且大小不为0,所以为真(1)
1
[root@test-1 scripts]# [ -e "$file1" ] && echo 1 || echo 0   #文件存在所以为真(1)
1
[root@test-1 scripts]# dir1=/etc
[root@test-1 scripts]# [ -e "$dir1" ] && echo 1 || echo 0
1

  特殊例子:如果变量不加双引号,结果可能就不正确:

[root@test-1 scripts]# echo $file7

[root@test-1 scripts]# [ -f $file7 ] && echo 1 ||echo 0
1  #明明$file 7不存在还返回1.
[root@test-1 scripts]# [ -f "$file7" ] && echo 1 ||echo 0
0

  范例2:把变量内容换成文件实体

[root@test-1 scripts]# [ -f /etc/services ] && echo 1 ||echo 0
1
[root@test-1 scripts]# [ -f /etc/service ] && echo 1 ||echo 0
0
[root@test-1 scripts]# [ -f "/etc/service" ] && echo 1 ||echo 0
0
[root@test-1 scripts]# [ -f "/etc/services" ] && echo 1 ||echo 0
1

 范例3:生产环境系统nfs启动脚本的条件测试的内容

[root@test-1 scripts]# more /etc/init.d/nfs
# Source networking configuration.
[ -f /etc/sysconfig/network ] &&  . /etc/sysconfig/network
#如果/etc/sysconfig/network 文件存在就加载文件
# Check for and source configuration file otherwise set defaults
[ -f /etc/sysconfig/nfs ] && . /etc/sysconfig/nfs
#如果/etc/sysconfig/nfs 文件存在就加载文件
特别提示:系统脚本是我们学习编程的第一标杆,要多参考

  范例4:多文件单中括号[ ]与或非测试
  可用与(-a和&&)、或(-o 和 ||)、非(!)蒋多个条件表达式连接起来,接着上面的变量测试。

[root@test-1 scripts]# [ -f "$file1" -o -e "$file2" ] && echo 1 || echo 0
1
[root@test-1 scripts]# [ -f "$file1" -a -e "$file2" ] && echo 1 || echo 0
1
[root@test-1 scripts]# [ -f "$file1" || -e "$file2" ] && echo 1 || echo 0
-bash: [: missing `]'         #用法不对了。你知道那不对么?
-bash: -e: command not found
0

提醒:前面我们已经讲解过了。
	1. "-a"和"-o"逻辑操作符号用于[ ]中使用
	2. "&&" 和 "||" 逻辑操作符号用于[[ ]]中使用。
	3. 注意括号两端,必须要有空格。
如果你很倔,非要单中括号加&&或||
[root@test-1 scripts]# [ -f "$file1" ]  ||  [ -e "$file2" ] && echo 1 || echo 0
1
[root@test-1 scripts]# [ -f "$file1" ]  &&  [ -e "$file2" ] && echo 1 || echo 0
1

 范例6:简易高效的文件判断例子

在做测试判断时,不一定非要按照前面的方法。直接用后者做测试判断有时更简洁。例如:
范例:
[root@test-1 scripts]# [ -f "$file1" ] && echo 1
1
[root@test-1 scripts]# [ -f "$file3" ] || echo 0
0

系统范例:/etc/init.d/nfs
[ -x /usr/sbin/rpc.nfsd ] || exit 5
[ -x /usr/sbin/rpc.mountd ] || exit 5
[ -x /usr/sbin/exportfs ] || exit 5

  范例7:学习问答案例:判断条件后面执行多条命令语句。

#[ 判断 ] || 命令 这种句式,怎么执行一组命令?类似 [ 判断 ] || (命令1 命令2 命令3 ) 这样的内容
#希望实现
#	if [条件]
#		then
#			do something
#	else
#		命令1
#		命令2
#		命令3
#	fi
#这样的效果?

#解答:
 #  可以使用条件表达式,大括号的用法,格式如下。当条件不成立时就会执行大括号内的所有命令内容:(用于脚本中)
[root@test-1 scripts]# cat test.sh 
[ 3 -ne 3 ] || { 
	echo "I am aa "
	echo "I am bb"
	exit 1

}

# 如果写在一行里面,里面的每个命令还需要用分号结尾,如下所示: [root@test-1 scripts]# [ 3 -ne 3 ] || { echo "I am aa ";echo "I am bb";exit 1;} I am aa I am bb logout #提示:本例的用法很简洁,但是理解起来不如if条件句容易,因此,请根据自身情况使用

 拓展

1) 结尾小括号用法:
[root@test-1 scripts]# [ 3 -eq 3 ] && echo "JDK安装成功"||(echo "jdk安装失败,请检查"&& exit 1)
JDK安装成功
[root@test-1 scripts]# [ 3 -eq 2 ] && echo "JDK安装成功"||(echo "jdk安装失败,请检查"&& exit 1)
jdk安装失败,请检查
2) 结尾中括号用法: [root@test-1 scripts]# [ 3 -eq 2 ] && echo "JDK安装成功"||[echo "jdk安装失败,请检查"&& exit 1] -bash: [echo: command not found
3) 结尾大括号用法: [ 3 -eq 3 ] && echo "JDK安装成功" || {echo "jdk安装失败,请检查"; exit 1} [ 3 -eq 3 ] && echo "JDK安装成功" || { echo "jdk安装失败,请检查" exit 1 }

  

 

原文地址:https://www.cnblogs.com/scajy/p/12807072.html