Here are some example shell scripts that should help you
learn.  I use '----B' on a line by itself to indicate the
beginning (and '----E' for the end) of a file.

-Derek



Print out some file types:
----B
#! /bin/sh

file=$1

if   test -f $file
then
  echo "$file is a regular file"
elif test -d $file
then
  echo "$file is a directory"
else
  echo "$file is not a directory or a file"
fi

----E
Example interaction:
$ file-type /
/ is a directory
$ file-type /etc/passwd
/etc/passwd is a regular file
$ file-type /dev/null
/dev/null is not a directory or a file

Count to 10 from whatever number you supply as an argument
----B
#! /bin/sh

# The value supplied at run time
cur_value=$1

# Count up to 10
end_value=10

# Did they supply an argument (see test(1))
if test ! "$cur_value"
then
  echo "You must supply an argument"
  exit 1
fi

# Is their argument less than the ending value
if test $end_value -lt $cur_value
then
  echo "$end_value is less than $cur_value.  Please pick a smaller number"
  exit 2
fi

# Count up
while test $cur_value -le $end_value
do
  echo $cur_value
  cur_value=`expr $cur_value + 1`
done

----E
Example interaction:
$ count-from
You must supply an argument
$ count-from 2
2
3
4
5
6
7
8
9
10
$

Print out the number of processes each user is running:
----B
#! /bin/sh

# This takes care of solaris so we can use the BSD ps
# on either OS
PATH=/usr/ucb:$PATH

user_list=$*

# cycle over the list of logins
for login in $user_list
do
  # We did a more complicated version of this in the wed section.
  # This way solves the same problem but doesn't involve hard awk
  # syntax
  num=`ps auxww | grep "^$login " | awk '{ print $2 }' | wc -l`
  # if $login has no pids, num will be set to "", so we test that
  if test ! "$num"
  then
    num=0
  fi
  echo "$num pid[s] are $login's"
done

----E
Example interaction:
$ num-pids df leffert
       9 pid[s] are df's
       2 pid[s] are leffert's
$

Spit vulgarities out at people who run your shell scripts:
----B
#! /bin/sh

# whoami is /usr/ucb/whoami on solaris
PATH=/usr/ucb:$PATH

me=df

case `whoami`
in
  $me)
    echo "Welcome $me"
    ;;
  root)
    echo "root, you should know better"
    ;;
  *)
    echo '@#*&^#$'
    ;;
esac

----E

