Unix Shell Scripts

Example of "foreach" loop.

internet% cat checkActivityForDay.sio

#!/bin/csh -f
#################################################################
# This script shows an example of a "foreach" loop
# Author: Siomara PAntarotto
# Usage : checkActivityForDay.sio x y z (where x, y, and z are
#         numbers between 1 and 7.
#################################################################

foreach token($argv)
    echo -n "Checking activity for day: " # -n supresses new line
    echo $token
    if ( $token == 1) then
        echo "SUNDAY - LET'S PARTY"
    else if ( $token == 2 ) then
        echo "MONDAY - LET'S WORK"
    else if ( $token == 3 ) then
        echo "TUESDAY - LET'S WORK"
    else if ( $token == 4 ) then
        echo "WEDNESDAY - LET'S WORK"
    else if ( $token == 5 ) then
        echo "THURSDAY - LET'S WORK"
    else if ( $token == 6 ) then
        echo "FRIDAY - LET'S WORK & PARTY"
    else if ( $token == 7 ) then
        echo "SATURDAY - LET'S PARTY"
    else
        echo "Sorry, You must inform a number between 1 and 7."
    endif
    echo ""
end

Results:

internet% checkActivityForDay.sio 1 2 3 4 5 6 7 8 9

Checking activity for day: 1
SUNDAY - LET'S PARTY

Checking activity for day: 2
MONDAY - LET'S WORK

Checking activity for day: 3
TUESDAY - LET'S WORK

Checking activity for day: 4
WEDNESDAY - LET'S WORK

Checking activity for day: 5
THURSDAY - LET'S WORK

Checking activity for day: 6
FRIDAY - LET'S WORK & PARTY

Checking activity for day: 7
SATURDAY - LET'S PARTY

Checking activity for day: 8
Sorry, You must inform a number between 1 and 7.

Checking activity for day: 9
Sorry, You must inform a number between 1 and 7.

internet%

 

Example of "while" loop.

internet% cat factorial.sio

#!/bin/csh -f
#######################################################################
# This script shows an example of a "while" loop.
# Calculates factorial of number passed as parameter
# Author: Siomara Pantarotto
# Usage : factorial.sio x (where x is a valid number)
#######################################################################

@ number = $1
@ total = 1

echo -n "Factorial of $number is : "

if ( $number == 0 ) then
    echo $total
    exit 0
endif

set sign = ""
while ( $number >= 1 )
    echo -n $sign$number
    @ total *= $number
    @ number--
    set sign = "."
end
echo " = "$total

Results:

internet% factorial.sio 0
Factorial of 0 is : 1
internet% factorial.sio 1
Factorial of 1 is : 1 = 1
internet% factorial.sio 2
Factorial of 2 is : 2.1 = 2
internet% factorial.sio 3
Factorial of 3 is : 3.2.1 = 6
internet% factorial.sio 4
Factorial of 4 is : 4.3.2.1 = 24
internet% factorial.sio 5
Factorial of 5 is : 5.4.3.2.1 = 120
internet% factorial.sio 6
Factorial of 6 is : 6.5.4.3.2.1 = 720
internet% factorial.sio 7
Factorial of 7 is : 7.6.5.4.3.2.1 = 5040
internet% factorial.sio 8
Factorial of 8 is : 8.7.6.5.4.3.2.1 = 40320
internet% factorial.sio 9
Factorial of 9 is : 9.8.7.6.5.4.3.2.1 = 362880
internet% factorial.sio 10
Factorial of 10 is : 10.9.8.7.6.5.4.3.2.1 = 3628800

1