Linux下shell的条件执行,[Linux Shell学习系列六]Shell的条件执行——1条件测试

#1.使用-e选项,如果文件存在为真

$ test-e /bin/cp && echo "The command $_ found." || echo "The command $_ not found."The command/bin/cpfound.

#2.使用-f选项,文件为常规文件且存在时为真

$ test-f /etc/resolv.conf && echo "The file $_ found." || echo "The file $_ not found."Thefile /etc/resolv.conf found.

#3.使用-d选项,文件为目录且存在时为真

$ test-d /local && echo "The directory $_ found." || echo "The directory $_ not found."The directory/local not found.

#4.使用-c选项,文件为特殊字符文件且存在时为真

$ test-c /dev/zero && echo "The file $_ is a character spetial." || echo "The file $_ is not a character spectial."Thefile /dev/zero is a character spetial.

$ test-c /bin/ls && echo "The file $_ is a character spetial." || echo "The file $_ is not a character spectial."Thefile /bin/lsis not a character spectial.

#5.使用-b选项,文件为特殊块文件且存在时为真

$ test-b /dev/sda && echo "The file $_ is a block spetial." || echo "The file $_ is not a block spectial."Thefile /dev/sda is not a block spectial.

$ test-b /bin/cp && echo "The file $_ is a block spetial." || echo "The file $_ is not a block spectial."Thefile /bin/cpis not a block spectial.

#6.使用-p选项,文件为命名管道且存在时为真

$ test-p /dev/initctl && echo "The file $_ is a named pipe." || echo "The file $_ is not a named pipe."Thefile /dev/initctl is a named pipe.

$ test-p /bin/grep && echo "The file $_ is a named pipe." || echo "The file $_ is not a named pipe."Thefile /bin/grepis not a named pipe.

#7.使用-S选项,文件为套接字文件且存在时为真

$ test-S /dec/log && echo "The file $_ is a socket." || echo "The file $_ is a socket."Thefile /dec/log is a socket.

#8.使用-L选项,文件为符号链接且存在时为真(与-h相同)

$ls -ll /bin/shlrwxrwxrwx.1 root root 4 Nov 26 06:24 /bin/sh ->bash

$ test-L /bin/sh && echo "The file $_ is a symbolic link." || echo "The file $_ is a symbolic link."Thefile /bin/shis a symbolic link.

#9.使用-h选项,文件为符号链接且存在时为真(与-h相同)

$ test-h /bin/sh && echo "The file $_ is a symbolic link." || echo "The file $_ is a symbolic link."Thefile /bin/shis a symbolic link.

#10.使用-g选项,文件存在且设置了sgid位时为真

$ test-g /bin/mount && echo "The file $_ has sgid bit." ||echo "The file $_ has no sgid bit."Thefile /bin/mounthas no sgid bit.

#11.使用-u选项,文件存在且设置了suid位时为真

$ test-u /bin/mount && echo "The file $_ has suid bit." ||echo "The file $_ has no suid bit."Thefile /bin/mounthas suid bit.

#12.使用-r选项,文件存在且可读时为真

$ test-r /proc/meminfo && echo "The file $_ is readble." ||echo "The file $_ is not readble."Thefile /proc/meminfo is readble.

#13.使用-w选项,文件存在且可写时为真

$ test-w /proc/meminfo && echo "The file $_ is writable." ||echo "The file $_ is not writable."Thefile /proc/meminfo is not writable.

#14.使用-x选项,文件存在且可执行时为真

$ test-x /bin/cp && echo "The file $_ is excutable." ||echo "The file $_ is not excutable."Thefile /bin/cpis excutable.

$ test-x /proc/meminfo && echo "The file $_ is excutable." ||echo "The file $_ is not excutable."Thefile /proc/meminfo is not excutable.

#15.使用-s选项,文件存在且不为空时为真

$ test-s /ect/inittab && echo "The file $_ is not empty." || echo "The file $_ is empty."Thefile /ect/inittab is empty.

#16.使用-t选项(test -t ),文件描述符fd已打开且引用了一个终端则为真

(后续介绍)

#17. -nt ,FILE1比FILE2新则为真

$ ll

total12

-rw-rw-r--. 1 user1 user1 19 May 15 10:42a.txt-rw-rw-r--. 1 user1 user1 12 May 15 09:36b.txt-rw-rw-r--. 1 user1 user1 6 May 15 09:36c.txt

$ test a.txt-nt b.txt && echo "a.txt is newer than b.txt." || echo "a.txt is older than b.txt."a.txt is newer than b.txt.

#18. -ot ,FILE1比FILE2旧则为真

$ test a.txt-ot b.txt && echo "a.txt is older than b.txt." || echo "a.txt is newer than b.txt."a.txt is newer than b.txt.

#19. -ef ,FILE1有硬链接到FILE2则为真(如果FILE1和FILE2指向相同的设备和节点号则返回为真。)

$ln a.txt a.ln$ls -l a.txt a.ln

-rw-rw-r--. 2 user1 user1 19 May 15 10:42 a.ln

-rw-rw-r--. 2 user1 user1 19 May 15 10:42a.txt

$ test a.ln -ef a.txt && echo "True" || echo "False"True

$ test a.ln -ef b.txt && echo "True" || echo "False"False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值