Shell file, directory exists or has no certain permissions of code

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

With the shell script to determine whether a file or directory exists, to judge whether a certain privileges.

#!/bin/sh 
#check file or directory or permission
#edit www.jbxue.com

myPath="/var/log/httpd/" 
myFile="/var /log/httpd/access.log" 
#The -x parameter to judge $myPath there exists and that you have permission to execute 

if [ ! -x "$myPath"]; then 
mkdir "$myPath" 
fi 
#The -d parameter to judge whether the existence of the $myPath here 
if [ ! -d "$myPath"]; then 
mkdir "$myPath" 
fi 
#The -f parameter to judge whether the existence of the $myFile here 
if [ ! -f "$myFile" ]; then 
touch "$myFile" 
fi 
#Other parameters and -n, -n is to judge whether a variable is a value 
if [ ! -n "$myVar" ]; then 
echo "$myVar is empty" 
exit 0 
fi 
#Two variables to determine whether equal 
if [ "$var1" = "$var2" ]; then 
echo ‘$var1 eq $var2′ 
else 
echo ‘$var1 not eq $var2′ 
fi 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Hugh at November 11, 2013 - 1:17 AM