Thursday, November 8, 2012

Shell scripting if else example

Script example that accepts one parameter.

Example:

./my-script.sh param1 # - string comparison
./my-script.sh 100 - # integer comparison

Integer comparison

if [ $1 -eq 100 ]; then
echo "Parameter equals 100"
else
echo "Parameter is not equal to 100"
fi

# Other possible values for comparison operator: -qe, -ne, -gt

String comparison

if [[ "$1" == "param1" ]]; then
echo "Comparison match"
else
echo "String does not match / parameter invalid, etc"
fi

No comments: