1. 程式人生 > >shell之判斷文件是否存在

shell之判斷文件是否存在

判斷 else blog logs 執行權限 其他 color not bsp

#!/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

shell之判斷文件是否存在