您的位置:首页 > 运维架构 > Shell

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

2012-04-13 08:38 519 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: