现在的位置: 首页 > 综合 > 正文

「Shell」Shell 脚本学习──文件存在判断

2018年04月07日 ⁄ 综合 ⁄ 共 527字 ⁄ 字号 评论关闭

Shell 文件判断,判断文件或路径是否存在
#!/bin/sh

NovaPath=”/var/log/httpd/”
NovaFile=”/var /log/httpd/access.log”

#这里的-x 参数判断文件夹$NovaPath是否存在并且是否具有可执行权限
if [ ! -x "$NovaPath"]; then
mkdir “$NovaPath”
fi

#这里的-d 参数判断路径$NovaPath是否存在
if [ ! -d "$NovaPath"]; then
mkdir “$NovaPath”
fi

#这里的-f参数判断文件$NovaFile是否存在
if [ ! -f "$NovaFile" ]; then
touch “$NovaFile”
fi

#其他参数还有-n,-n是判断一个变量是否有值
if [ ! -n "$NovaVar" ]; then
echo “$NovaVar is empty”
exit 0
fi

#两个变量判断是否相等
if [ "$var1" = "$var2" ]; then
echo ‘$var1 eq $var2′
else
echo ‘$var1 not eq $var2′
fi

 

来自:

http://hi.baidu.com/sshow224/blog/item/c6ce75c35d3fde5cb219a875.html

抱歉!评论已关闭.