#! /bin/ksh
# ex1 - Module 2 Unit new.5 Exersise ex1
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Tue Aug 17 16:56:05 CDT 1999
# Unit 5 Exercise 1 Modules 2
# write a program which loops through
# all positional parameters on the command line,
# printing the number of their position and their value.
# while [ "$1" != "" ] do print ; incr+1 ; shift done
n=1 # shorthand for i in $* do print ; incr ; done
for i
do
print "Arg $n: $i"
((n=n+1))
done
#! /bin/ksh
# ex2 - Module 2 Unit new.5 Exersise ex2
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Tue Aug 17 16:58:53 CDT 1999
# Unit 5 Exercise 2 Module 2
# write a program which waits until a specified user
# logs on the system. The following specifications are provided.
# if a -tn option is specified the program sleeps for n minutes
# the default is 60 seconds. You will have to use do let command
# to convert minutes to seconds for the sleep command
# the program makes sure the user has supplied a user name
# the program checks to make sure the user can log on this system
# by checking for the user in the /etc/passwd file.
# if the user is already log on, print a message to that that, exit
# if the user is not log on, sleep for the number of specified seconds
# and check again. Keep checking the until the user at logs on or
# kills signal.
TIME=60 # minimum time
USAGE="Usage: $0 [-t minutes] user"
# parse command line options
while getopts :t: OPTION_FLAG
do
case $OPTION_FLAG in
t) ((TIME=OPTARG*60));;
\?) print $USAGE
exit 2;;
esac
done
# shift out the options to get the user
shift $(expr $OPTIND - 1)
if [ $# -eq 0 ] # user name was not suppled
then
print "$USAGE"
exit 3
fi
# varify user has system access
grep "^${1}:" /etc/passwd > /dev/null
if [ $? -ne 0 ]
then
print "$1 is not allowed on this system."
exit 1
fi
# is the user already logged on?
if who | grep $1 > /dev/null
then
print "$1 is already logged on to the system"
exit
fi
# enter a loop and wait until the user logs on
print -n "Waiting for $1 to log on"
while true
do
who | grep $1 > /dev/null
if [ $? -ne 0 ]
then
print -n " ."
sleep $TIME
else
tput flash # Ring the Bell
print "\n $1 just logged on"
exit
fi
done
#! /bin/ksh
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 16:39:08 CDT 1999
# Unit 5 Exercise 3 Module 2
# write a program named runcmd which allows of the user
# to pass a flag to specifie which Unix command
# to run. The program will accept the following commands
# -l runs ls, -w runs who, -d runs date if no flags are passed
# or an unauthorized flag is passed print error, exit.
USAGE="$0: -[lwd]"
# check to make sure that option was given
if [ $# -eq 0 ]
then
print "$USAGE"
exit 1
fi
while getopts :lwd OPTION_FLAG
do
case $OPTION_FLAG in
l) ls ;;
w) who ;;
d) date ;;
/?) print "$USAGE" ; exit 2 ;;
esac
done
exit 0 # success
#! /bin/ksh
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 16:54:12 CDT 1999
# Unit 5 Exercise 4 Module 2
# use the select statement to print a menu of
# date, who, and ls commands. Perform the
# appropriate command for the selection.
PS3="selection? "
select ans in date who ls
do
case $REPLY
in
1 | date) date ;;
2 | who) who ;;
3 | ls) ls ;;
*) print "invalid input" ;;
esac
done
#! /bin/ksh
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 16:54:12 CDT 1999
# Unit 5 Exercise 5 Module 2
# use the select statement to print a menu of
# date, who, ls, and quit commands. Perform the
# appropriate command for the selection.
PS3="selection? "
select ans in date who ls quit
do
case $REPLY
in
1 | date) date ;;
2 | who) who ;;
3 | ls) ls ;;
4 | quit) exit ;;
*) print "invalid input" ;;
esac
done
#! /bin/ksh
# ex6 -
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 17:08:12 CDT 1999
# unit 5 exercise 6 module 2
# the database file contains a list of names and addresses
# separated by |'s. Write a shell program
# which generates mailing labels for this file. Hints, you'll
# have to change be IFS the new shell program
# and use a while loop to read each line from the database.
# head -5 database | sort.... grabs first 5 lines
IFS="|"
exec < database # redefined were standard input comes from
while read name address city state zip
do
print "$name"
print "$address"
print "$city, $state $zip"
print # print a blank line-may be print more than \
line based on the size of your labels
done
#! /bin/ksh
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 18:10:46 CDT 1999
# Unit 5 exercise 7
# using co-processes write two shell scripts.
# The first shell script, dolabel, starts of the server process named
# getdata. The dolabel program ask the user for a two letter state
# abbreviation, make sure it isn't uppercase,
# and then passes the state abbreviation to getdata.
# Getdata lines all the records in the database file that match
# the state abbreviation and passes them back to dolabel.
# dolabel receives records from getdata and prints
# mailing labels for those states. If getdata cannot
# find the records in the database that match the state
# it was sent, then it should send back some indication
# to dolabel so labels and not printed.
# print labels for records received from server
# set variable to all uppercase
typeset -u state_abbr
# start up server
getdata |&
# ask user for this state
print -n " Enter two letter state abbreviation: "
read state_abbr
# send to getdata
print -p $state_abbr
# read the data back
IFS="|"
while read -p name address city state zip
do
if [ "$name" = "NULL" ]
then
print "cannot find a record for state abbreviation $state_abbr"
exit 1
fi
print $name
print $address
print "$city, $state $zip"
print
done
#! /bin/ksh
# Created by: Scott Kinsey $LOGNAME: train54
# Created on: Fri Aug 20 18:22:25 CDT 1999
# unit 5 exercise 8
# database server
read state
status=0
# get the names
exec < database
while read info
do
echo "$info" | grep "${state}" > /dev/null
if [ $? -eq 0 ]
then
print $info
status=1
fi
done
if [ "$status" -eq 0 ]
then
print "NULL"
fi