shell判斷文件,目錄是否存在或者具有權限
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #這里的-x 參數判斷$myPath是否存在並且是否具有可執行權限 if [ ! -x "$myPath"]; then mkdir "$myPath" fi
#這里的-d 參數判斷$myPath是否存在 if [ ! -d "$myPath"]; then mkdir "$myPath" fi #這里的-f參數判斷$myFile是否存在 if [ ! -f "$myFile" ]; then touch "$myFile" fi #其他參數還有-n,-n是判斷一個變量是否是否有值 if [ ! -n "$myVar" ]; then echo "$myVar is empty" exit 0 fi #兩個變量判斷是否相等 if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2' else echo '$var1 not eq $var2' fi