How to Evaluate an Expresion in Shell Scripting Language |
|
Saturday, 23 January 2010 |
Next example evaluates if a=100, and if is true, it will echo a:
If evaluation example #!/bin/sh a=100 if [ "$a" -eq 100 ] then fi
Here are some evaluation operators (comparissons): -eq --- is equal to -ne --- is not equal to -gt --- is greater then -ge --- is greater then or equal to -lt --- is less then -le --- is less then or equal to = --- is equal to, valid for string comparisons.
File operations (verify if): -f --- file exists and is not a directory -d --- directory exists -s --- file exists and is not empty -x --- file is executable -r --- file is readable -w --- file is writable
The followig example print "File exists" message, if a file install.log exists in the same directory as the script:
example2.sh #!/bin/sh echo "Now I will verify if file install.log exists:" if [ -f "install.log" ] then fi
You can also use else: example3.sh $CD=/home/user/tmp cd $CD LOCATION=`pwd` if [ "$LOCATION" = "$CD" ] then echo "Deleting from: "$CD rm -Rf * else echo "Error. Path for deleting files not found, exiting!" fi
|
Last Updated ( Saturday, 26 March 2011 )
|