linux之shell命令——条件判断(&& || test)

&& 和 ||

&&:用来执行条件成立后执行的命令
||:用来执行条件不成立后的执行命令

示例:
linux之shell命令——条件判断(&& || test)

[[email protected] mnt]# id toto   # 用户toto不存在无法查看
id: toto: no such user
[[email protected] mnt]# id toto &> /dev/null && echo yes || echo no  # 判断用户是否存在
no    # 由于用户不存在 条件不成立 所以输出no
[[email protected] mnt]# useradd toto   # 创建用户之后 用户存在 
[[email protected] mnt]# id toto &> /dev/null && echo yes || echo no  # 条件成立 输出yes
yes

测试:检测是否能够ping通 主机 172.25.47.204 ,通则输出yes,不通则输出no
linux之shell命令——条件判断(&& || test)

编写脚本,用以检测网络能否ping通
可以ping通输出此ip is up;ping不通输出 此ip is down

linux之shell命令——条件判断(&& || test)
linux之shell命令——条件判断(&& || test)

test 命令

test命令通常做判断, test 命令和 [ ] 等同

 [ "$A" = "$B"  ]        	                ##是否相等
 [  "$A" != "$B"  ]        	                ##是否不相等
 [  "$A" -eq "$B"  ]      	                ##是否相等
 [  "$A" -ne "$B"  ]       	                ##是否不相等
 [  "$A" -le "$B"  ]       	                ##是否小于等于
 [  "$A" -lt "$B"  ]                        ##是否小于
 [  "$A" -ge "$B "  ]                       ##是否大于等于
 [  "$A" -gt "$B"  ]                        ##是否大于
 [  "$A" -ne "$B" -a "$A" -gt "$B"  ]       ##A 不等于 B 并且 A 大于B
 [  "$A" -ne "$B" -o "$A" -gt "$B"  ]       ##A不等于B 或者  A大于B
 [  -z "$A"  ]                              ##是否为空
 [  -n "$A"  ]                              ##是否不为空
示例

1 test命令通常做判断, test 命令和 [ ] 等同

[[email protected] mnt]# a=1
[[email protected] mnt]# b=1
[[email protected] mnt]# test "$a" = "$b" && echo yes || echo no
yes
[[email protected] mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# test "$a" != "$b" && echo yes || echo no
no
[[email protected] mnt]# [ "$a" != "$b" ] && echo yes || echo no
no

2 -eq ##等于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
yes

3 -ne ##不等于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[[email protected] mnt]# b=2
[[email protected] mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes

4 -le ##小于等于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=2
[[email protected] mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# b=0
[[email protected] mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no

5 -lt ##小于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=2
[[email protected] mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no

6 -ge ##大于等于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=2
[[email protected] mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[[email protected] mnt]# a=2
[[email protected] mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[[email protected] mnt]# a=3
[[email protected] mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes

7 -gt ##大于

[[email protected] mnt]# a=1
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
no
[[email protected] mnt]# a=2
[[email protected] mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes

8 -a ##同时满足

[[email protected] mnt]# a=1
[[email protected] mnt]# b=2
[[email protected] mnt]# c=3
[[email protected] mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
yes
[[email protected] mnt]# b=1
[[email protected] mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
no

9 -o ##两个条件满足其中一个就可以

[[email protected] mnt]# a=1
[[email protected] mnt]# b=2
[[email protected] mnt]# c=3
[[email protected] mnt]# [ "$a" -lt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
yes
[[email protected] mnt]# [ "$a" -gt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
no

10 -z ##是否为空

[[email protected] mnt]# c=''
[[email protected] mnt]# [ -z  "$c" ] && echo yes || echo no
yes
[[email protected] mnt]# c=2
[[email protected] mnt]# [ -z  "$c" ] && echo yes || echo no
no

11 -n ##不为空

[[email protected] mnt]# c=''
[[email protected] mnt]# [ -n  "$c" ] && echo yes || echo no
no
[[email protected] mnt]# c=2
[[email protected] mnt]# [ -n  "$c" ] && echo yes || echo no
yes
一个文件具体属于什么类型
[ -f “file” ]                 ##普通文件为真
[ -e “file” ]                 ##文件存在为真
[ -L “file” ]                 ##软链接为真
[ -S “file” ]                 ##套节字为真
[ -b “file” ]                 ##硬盘设备为真
[ -d “file” ]                 ##目录为真
[ -c “file” ]                 ##字符设备为真
[ “file1” -ef “file2” ]       ##两文件为同一个文件为真
[ “file1” -nt"file2" ]        ##文件1比文件2创建的晚为真
[ “file1” -ot “file2” ]       ##文件1比文件2创建的早为真

示例:
[ “file1” -ef “file2” ] ##两文件为同一个文件为真

[[email protected] mnt]# touch file1
[[email protected] mnt]# ln file1 file2
[[email protected] mnt]# ls 
file1  file2  ping.sh
[[email protected] mnt]# ls -i
8845808 file1  8845808 file2  8845813 ping.sh
[[email protected] mnt]# [ "file1" -ef "file2" ] && echo yes || echo no
yes
[[email protected] mnt]# [ "file1" -ef "ping.sh" ] && echo yes || echo no
no

[ “file1” -nt"file2" ] ##文件1比文件2创建的晚为真
[ “file1” -ot “file2” ] ##文件1比文件2创建的早为真

[[email protected] mnt]# ll
total 0
-rw-r--r--. 1 root root 0 May 20 06:25 file1
-rw-r--r--. 1 root root 0 May 20 06:27 file2
[[email protected] mnt]# [ "file1" -nt "file2" ] && echo yes || echo no
no
[[email protected] mnt]# [ "file1" -ot "file2" ] && echo yes || echo no
yes
测试1 编写脚本 判断输出文件类型

linux之shell命令——条件判断(&& || test)
linux之shell命令——条件判断(&& || test)

测试2 编写脚本 创建或者删除用户

当创建用户时,用户存在则输出用户存在,不存在则建立用户
当删除用户时,用户存在则删除用户,用户不存在则输出用户不存在

linux之shell命令——条件判断(&& || test)
linux之shell命令——条件判断(&& || test)