Friday 26 October 2012

"IF" on Linux shell script


VALID_PASSWORD="secret" #this is our password.

echo "Please enter the password:"
read PASSWORD

if [ "$PASSWORD" == "$VALID_PASSWORD" ]; then
 echo "You have access!"
else
 echo "ACCESS DENIED!"
fi
 
 
Comparisons:
-eqequal to
-nenot equal to
-ltless than
-leless than or equal to
-gtgreater than
-gegreater than or equal to

File Operations:
-sfile exists and is not empty
-ffile exists and is not a directory
-ddirectory exists
-xfile is executable
-wfile is writable
-rfile is readable
-ntests to see if the argument is non empty   

X=""
# -n tests to see if the argument is non empty
if [ -n $X ]; then  
 echo "the variable X is not the empty string"
fi

if [ "$AGE" -lt 20 ] || [ "$AGE" -ge 50 ]; then
 echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
 echo "You are in your 20s"
fi 

if [[ $count -gt 0  &&  $somevar != $var ]]; then
...no brackets inside, only double [[  ]]
fi



if [ $count -gt 0 ] && [ $somevar != $var ]; then
...do something
fi


No comments:

Post a Comment