Tuesday 7 May 2013

for and while loops


The for statement

Like other programming languages, UNIX has a for statement (count controlled loop). For example:

for i in {1..5}
do
   echo $i
done

The loop can also be reversed e.g. for i in {5..1}

We can also have an infinite loop – but with a break:

for ((;;))
do
   echo "Enter value "
   read value
   if (($value == 99))
      then
          break
   fi
done

Nested loops are also possible e.g.

            for i in { }
               for j in {}   etc.


Exercise 3: Using a nested for loop, write a script which displays the 12 times tables e.g. 1 x 1, etc followed by 2 x 1 until 12 x 1 to 12 x 12. After each table, set a pause (sleep) and clear the screen and continue.


Assessment 2

Time: approx 1 hour

Topics

  • Awk – lab 3
  • sed – lab 4
  • use of test command
  • at & cron
  • run-levels + use of inittab
  • use of /etc/sudoers
  • for statement – lab 10


As before, you may not use class or lab notes (including electronic version) but you may use any system resource including man pages, google, etc.

No comments:

Post a Comment