Shell条件判断语句上

一 按照文件类型进行判断


 
 
二 两种判断格式
test -e /root/install.log
[ -e /rout/install.log ]
第一个判断命令如果正确执行,则打印“yes”,否则打印“no”
[ -d /root ] && echo "yes" || "no"
 
三 实战
[root@localhost ~]# ls
abc canyls Downloads Pictures Templates
abcd ChangeLog-2.6.0 initial-setup-ks.cfg Public test.txt
anaconda-ks.cfg Desktop linux-2.6.39.tar.bz2 sh Videos
cangls Documents Music student.txt XshellXftpPortable.zip
[root@localhost ~]# [ -e /root/test.txt ]
[root@localhost ~]# [ -e /root/test.txt2 ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -d /root ] && echo "yes" || "no"
yes
[root@localhost ~]# [ -e /root/test.txt ] && echo "yes" || "no"
yes
 
四 按照文件权限判断


 
 
五 实战
[root@localhost ~]# [ -w /root/test.txt ] && echo "yes" || "no"
yes

猜你喜欢

转载自cakin24.iteye.com/blog/2392869