Conditional statement Flashcards
primary expression:
if
combining expression:
if
checks for the existence of a file
!/bin/bash
anny ~> cat msgcheck.sh
echo “This scripts checks the existence of the messages file.”
echo “Checking…” if [-f /var/log/messages]
then echo “/var/log/messages exists.”
fi
echo echo “…done.”
anny ~> ./msgcheck.sh
This scripts checks the existence of the messages file.
Checking…
/var/log/messages exists.
…done.
Checking shell options
These lines will print a message if the noclobber #option is set:
if [-o noclobber]
then echo “Your files are protected against accidental overwriting using redirection.”
fi
Testing exit status
anny ~> if [$? -eq 0]
More input> then echo ‘That was a good job!’
More input> fi
That was a good job!
numerical comparisons
anny > num=wc -l work.txt
anny > echo $num 201
anny > if [“$num” -gt “150”] More input> then echo ; echo “you’ve worked hard enough for today.” More input> echo ; fi
you’ve worked hard enough for today.
String comparisons-Standard
if [”$(whoami)” != ‘root’]; then
echo “You have no permission to run $0 as non-root user.”
exit 1;
fi
String comparison-Shortened
[”$(whoami)” != ‘root’] && ( echo you are using a non-privileged account; exit 1 )
String comparison-Regular Expression
anny > gender=”female”
anny > if [[“$gender” == f*]]
More input> then echo “Pleasure to meet you, Madame.”; fi
Pleasure to meet you, Madame.
String comparison- Programmer
test “$(whoami)” != ‘root’ && (echo you are using a non-privileged account; exit 1)
if/then/else constructs
freddy scripts> gender=”male”
freddy scripts> if [[“$gender” == “f*”]]
More input> then echo “Pleasure to meet you, Madame.”
More input> else echo “How come the lady hasn’t got a drink yet?”
More input> fi How come the lady hasn’t got a drink yet?
Single Bracket v.s. Double Bracket
[[ prevents word splitting of variable values. eg. VAR=”var with spaces”, you do not need to double quote $VAR in a test - eventhough using quotes remains a good habit.
[[ prevents pathname expansion, so literal strings with wildcards do not try to expand to filenames.
Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[“value” == val*]].
Checking command line arguments
!/bin/bash
anny ~> cat weight.sh
This script prints a message about your weight if # you give it your
weight in kilos and height in centimeters. weight=”$1”
height=”$2”
idealweight=$[$height - 110]
if [$weight -le $idealweight] ; then
echo “You should eat a bit more fat.”
else
echo “You should eat a bit more fruit.”
fi
Testing the number of arguments
!/bin/bash
anny ~> cat weight.sh
This script prints a message about your weight if #you give it your
weight in kilos and height in centimeters.
if [! $# == 2]; then
echo “Usage: $0 weight_in_kilos length_in_centimeters”
exit
fi
weight=”$1”
height=”$2”
idealweight=$[$height - 110]
if [$weight -le $idealweight] ; then
echo “You should eat a bit more fat.”
else
echo “You should eat a bit more fruit.”
fi
Testing that a file exists
!/bin/bash
This script gives information about a file.
FILENAME=”$1”
echo “Properties for $FILENAME:”
if [-f $FILENAME]; then
echo “Size is $(ls -lh $FILENAME | awk ‘{ print $5 }’)”
echo “Type is $(file $FILENAME | cut -d”:” -f2 -)”
echo “Inode number is $(ls -i $FILENAME | cut -d” “ -f1 -)”
echo “$(df -h $FILENAME | grep -v Mounted | awk ‘{ print “On”,$1”, \
which is mounted as the”,$6,”partition.”}’)”
else
echo “File does not exist.”
fi