Monday, March 22, 2010

condition & Loops - Deciding flow in UNIX script

IF

The basic type of condition is "if".
if [ $? -eq 0 ] ; then
 print we are okay
else
 print something failed
fi
IF the variable $? is equal to 0, THEN print out a message. Otherwise (else), print out a different message. FYI, "$?" checks the exit status of the last command run. The final 'fi' is required. This is to allow you to group multiple things together. You can have multiple things between if and else, or between else and fi, or both.
You can even skip the 'else' altogether, if you dont need an alternate case.
if [ $? -eq 0 ] ; then
 print we are okay
 print We can do as much as we like here
fi


CASE

The case statement functions like 'switch' in some other languages. Given a particular variable, jump to a particular set of commands, based on the value of that variable. While the syntax is similar to C on the surface, there are some major differences;
  • The variable being checked can be a string, not just a number
  • There is no "fall through" with ;;. You hit only one set of commands.. UNLESS you use ";&" instead of ";;'
  • To make up for no 'fall through', you can 'share' variable states
  • You can use WILDCARDS to match strings

echo input yes or no
read  answer
case $answer in
 yes|Yes|y)
  echo got a positive answer
  # the following ';;' is mandatory for every set
  # of comparative xxx)  that you do
  ;;
 no)
  echo got a 'no'
  ;;
 q*|Q*)
  #assume the user wants to quit
  exit
  ;;
  
 *)
  echo This is the default clause. we are not sure why or
  echo what someone would be typing, but we could take
  echo action on it here
  ;;
esac


Loops

WHILE

The basic loop is the 'while' loop; "while" something is true, keep looping. There are two ways to stop the loop. The obvious way is when the 'something' is no longer true. The other way is with a 'break' command.

keeplooping=1;
while [[ $keeplooping -eq 1 ]] ; do
 read quitnow
 if [[ "$quitnow" = "yes" ]] ; then
  keeplooping=0
 fi
 if [[ "$quitnow" = "q" ]] ; then
  break;
 fi
done


UNTIL

The other kind of loop in ksh, is 'until'. The difference between them is that 'while' implies looping while something remains true.
'until', implies looping until something false, becomes true
until [[ $stopnow -eq 1 ]] ; do
 echo just run this once
 stopnow=1;
 echo we should not be here again.
done


FOR

A "for loop", is a "limited loop". It loops a specific number of times, to match a specific number of items. Once you start the loop, the number of times you will repeat is fixed. The basic syntax is

for var in one two three ; do
 echo $var
done
Whatever name you put in place of 'var', will be updated by each value following "in". So the above loop will print out
one
two
three
But you can also have variables defining the item list. They will be checked ONLY ONCE, when you start the loop.
list="one two three"
for var in $list ; do
 echo $var
 # Note: Changing this does NOT affect the loop items
 list="nolist"
done
The two things to note are:
  1. It stills prints out "one" "two" "three"
  2. Do NOT quote "$list", if you want the 'for' command to use multiple items
If you used "$list" in the 'for' line, it would print out a SINGLE LINE, "one two three"

No comments:

Post a Comment