shell测试

测试符记录,便于记忆查找

文件类型测试:(若是文件变量的话要加双引号)
-f :(file)判断文件是否为普通文件
-d :(directory)判断是否目录

-b :(block file)判断是都块设备文件
-c :(char file)判断是都字符设备文件
-S :(socket file)判断是否socket文件
-p :(pipe file)判断是否管道文件
-h :(..)判断是都为符号链接

-L :(link file)文件存在且有符号链接
文件大小存在性:
-e :(exit file)判断文件或者目录是否存在
-s :(size file)文件或者或者目录存在且大小大于0

文件读写特性
-r :(readable file)判断文件是否有可读权限
-w :(writeable file)判断文件是否具有可写权限
-x :(executable file)判断文件是否具有可执行的权限

-g:判断文件是否具有sgid位
-u:判断文件是否有suid位

-k:判断是否设置了粘滞位,文件设置粘滞位后会被写入缓存

文件修改时间:
file1 -nt file2 : (newer than)判断file1是否 比file2 新
file1 -ot file2 : (oldder then)判断file1是否 比file2 旧
字符串测试操作符
 = 	 测试两个字符串是否相等(等价与 == )
 != 	 测试两个字符串是否不相等
 -z 	 测试字符串是空字符串(zero)
 -n 	 测试字符串是非空字符串(no zero)

 整数比较操作符

[  ]中使用	()或[[  ]]中使用
-eq				=			等于
-ne       		!=			不等于
-gt       		>			大于
-ge       		>=			大于等于
-lt       		<			小于
-le       		<=			小于等于

逻辑操作符

[  ]中使用	()或[[  ]]中使用
-a       	&&			逻辑与
-o			||			逻辑或
!			!			逻辑非	

if-else例子1

[ -d /home/jhnet ] && echo '/home/jhnet exist' || {
    echo '/home/jhnet not exist'
    exit 1
}

 if-else例子2

mv /home/jhnet/ROOT.war /u01/tomcat7070/webapps/
if [ $? -eq 0 ]
then
    echo 'deplory project to dir:'${projectDir}' success!'
else
    echo 'deplory project to dir:'${projectDir}' fail!'
    exit 1
fi
扫描二维码关注公众号,回复: 291319 查看本文章

猜你喜欢

转载自linhexiao.iteye.com/blog/2288726