CIT052 Index > Assignment 1

Assignment 1
Shell Script Examples

The object of this assignment is to familiarize you with the vi editor and see some shell scripts in action.

Create two files that contain the shell scripts similar to those in examples 2.2 and example 2.7 in the book (pages 39-40 and 64). These aren’t the same as the ones in the book; they have been modified to fix some errors and adapt the script to this assignment.

While you could just copy and paste the information from the web site, the information will stay with you longer if you type it yourself.

You will put the first script into a file named script1a.csh and the second script into a file named script1b.sh

Script 1a

#!/bin/csh -f

#   The Party Program - Invitations to friends from the "guest" file

set guestfile = ./guests
if (! -e "$guestfile") then
    echo "$guestfile:t non-existent"
    exit 1
endif

setenv PLACE "Sarotini's"

@ Time = `date +%l | sed 's/^0//'` + 1
set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )

foreach person ( `cat $guestfile` )
    if ( $person == root ) continue
    cat << FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring $food[1] and
anything else you would like to eat? Let me know if you can
make it. Hope to see you soon.

   Your pal,
   ellie@`hostname`

FINIS
    shift food
    if ($#food == 0) then
        set food = ( cheese crackers shrimp drinks \
            "hot dogs" sandwiches )
    endif
end

echo "Bye..."

Script 1b

#!/bin/bash

#   The Party Program - Invitations to friends from the "guest" file

guestfile=./guests
if [[ ! -e "$guestfile" ]]
then
    echo "${guestfile##*/} non-existent"
    exit 1
fi

export PLACE="Sarotini's"

(( Time=$(date +%l) + 1 ))

declare -a food=(cheese crackers shrimp drinks 'hot\ dogs' sandwiches )
declare -i n=0

for person in $(cat $guestfile)
do
    if [[ $person == root ]]
    then
        continue
    else
        #start of here document
        cat <<- FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring ${food[$n]} and
anything else you would like to eat? Let me know if you can
make it. Hope to see you soon.

   Your pal,
   ellie@`hostname`

FINIS
        n=n+1
        if (( ${#food[*]} == $n ))
        then
            n=0
        fi
    fi
done

echo "Bye..."

Notes

When You Finish

You will have two files named script1a.csh and script1b.sh. Use tar utility to package the two files into a single compressed, gzipped file (called a “tarball”). The tarball file will have your family name and first name separated by an underscore, followed by the assignment number. Thus:

tar cvzf eisenberg_david_1.tgz script1a.csh script1b.sh

Email the tarball file to the instructor.