Assignments

Copy this, adapted from watchfor [KP99] Section 5.3.

#!/bin/bash

# watchfor: watch for someone to log in

PATH=/bin:/usr/bin

case $# in
	0)	echo 'Usage: watchfor person' 1>&2; exit 1
esac

until who | egrep "$1"
do
	sleep 60
done

Then change watchfor so that multiple arguments are treated as different people, rather than requiring the user to type 'joe|mary'.

Copy this, adapted from watchwho [KP99] Section 5.3.

#!/bin/bash

# watchwho: watch who logs in and out

PATH=/bin:/usr/bin
new=/tmp/wwho1.$$
old=/tmp/wwho2.$$
>$old			# create an empty file

while :
do
	who >$new
	diff $old $new
	mv $new $old
	sleep 60
done | awk '/>/ { $1 = "in:	"; print }
            /</ { $1 = "out:	"; print }'

Write a version of watchwho that stores the who output as shell variables instead of files. Which version do you prefer? Which version runs faster?

Copy this, adapted from replace [KP99] Section 5.5.

#!/bin/bash

# replace: replace str1 in files with str2, in place

PATH=/bin:/usr/bin

case $# in
	0 | 1 | 2)	echo 'Usage: replace str1 str2 files' 1>&2; exit 1
esac

left="$1"; right="$2"; shift; shift
tmp=/tmp/overwr1.$$

for i
do
	sed "s@$left@$right@g" $i > $tmp; cp $tmp $i
done
rm $tmp

Can replace be used to change the variable i to index everywhere in a program? How could you change things to make this work?