一:文件测试

在很多时候,写脚本写要判断一个目录、文件是否存在,或者是否有执行权限,然后在进行下一步操作,以下就是判断文件是否存在等方法:

1、-e和-a----->判断文件是否存在

#!/bin/bash

file=/root/testfile

[[  -e "$file" ]]

sum=`echo $?`

if [[ !  "$sum" -eq 0 ]];then

echo "$file is noexit"

else

echo "$file is exit"

fi


2、-f:测试是否存在且为普通文件,是为真,否则为假

[root@localhost /]# cat  test.sh

#!/bin/bash

file=/root/testfile

[[  -f "$file" ]]

sum=`echo $?`

if [[ !  "$sum" -eq 0 ]];then

echo "$file is noexit"

else

echo "$file is exit"

fi

[root@localhost /]# touch  /root/testfile

[root@localhost /]# sh test.sh

/root/testfile is exit


3、-e:判断是否存在,可以包含文件和目录,也就是等于-f和-d联合起来

-f测试一个目录:

[root@localhost ~]# mkdir dir

[root@localhost ~]# vim test.sh

#!/bin/bash

file=/root/dir

[[  -f  "$file" ]]

sum=`echo $?`

if [[   "$sum" -eq 0 ]];then

echo "$file is a dir"

else

echo "$file is not dir"

fi

[root@localhost ~]# sh test.sh

/root/dir is not dir


-e测试一个目录:

[root@localhost ~]# cat test.sh

#!/bin/bash

file=/root/dir

[[  -e  "$file" ]]

sum=`echo $?`

if [[   "$sum" -eq 0 ]];then

echo "$file is a dir"

else

echo "$file is not dir"

fi

[root@localhost ~]# sh test.sh

/root/dir is a dir


4、-d:判断文件是否存在并且是一个目录:


5、-b:测试文件是否存在并且是否是一个块设备文件


6、-c:测试文件是否是否存在并且是否是一个字符设备文件


7、-h:测试文件是否存在并且是否是一个链接文件

[root@localhost ~]# vim test.sh

#!/bin/bash

file=/root/testfile

[[  -h  "$file" ]]

sum=`echo $?`

if [[   "$sum" -eq 0 ]];then

echo "$file is  h"

else

echo "$file is not  h"

fi


[root@localhost ~]# sh test.sh

/root/testfile is not  h


8、-g:测试文件是否存在并且是否是一个g管道文件


9、-S:测试文件是否存在并且是否是一个套接字文件


10:-r:试是否存在且其当前用户是否针对此文件有读取权限


11:-w:试是否存在且其当前用户是否针对此文件有读写权限


12:x:试是否存在且其当前用户是否针对此文件有执行权限

[root@localhost /]# ll /root/testfile

-rw-r--r-- 1 root root 0 May 23 07:52 /root/testfile

[root@localhost /]# vim test.sh

#!/bin/bash

file=/root/test.sh

[[  -x  "$file" ]]

sum=`echo $?`

if [[   "$sum" -eq 0 ]];then

echo "$file is not -x"

else

echo "$file is -x "

fi

[root@localhost /]# sh test.sh

/root/test.sh is not -x


13:-s:测试文件是否存在并且不空


二:双目测试:

Command1 || Command2:只运行命令1或者命令2,即命令1执行后的返回值为0则命令2而不执行,当命令1执行后的返回值不为零,即表示命令执行失败,才执行命令2

Command1 && Command2:当论命令1执行后返回值为0即命令1执行成功后才执行命令2,否则不执行命令2


[root@localhost ~]# file=123

[root@localhost ~]# [ -e $file ] || mkdir $file

[root@localhost ~]# ll -d  123

drwxr-xr-x 2 root root 4096 May 23 09:23 123

[root@localhost ~]# date

Fri May 23 09:23:36 CST 2014


举例1:当前路径有123,使用&&和||分别作测试文件是否存在,结果如下:

[root@localhost ~]# mkdir 123

[root@localhost ~]# file=123

[root@localhost ~]# echo  $file

123

[root@localhost ~]# [[ -e $file ]] ||  pwd

[root@localhost ~]# [[ -e $file  ]] &&  pwd

/root

    可以看出使用&&命令1执行成功就执行命令2,即列出当前目录,而使用||则不执执行命令2,因为命令1已经执行成功了,就不再执行命令2.


举例2:删除目录123,查看结果有什么区别:

[root@localhost ~]# rm -rf 123

[root@localhost ~]# [[ -e $file  ]] &&  pwd

[root@localhost ~]# [[ -e $file ]] ||  pwd

/root

    可以看出,此次与刚才完全相反,使用&&的时候命令1没有执行成功,因此命令2就没有执行,而使用||的时候由于命令1没有执行成功,所有就执行命令2,因此可以得出,&&依赖于命令1执行成功才执行命令2,而||则正好相反,命令1执行识别才执行命令2,命令1执行成功就不执行命令2了。


总结:&&和||都要获得第一个命令的执行结果,然后判断第二个命令是要执行,而&&是命令1执行成功才执行命令2,||是命令1执行失败才执行命令2,理论上也就是&&要么都执行要么都不执行,||是要么命令1执行要么命令2执行