#! /bin/sh # Question 5: # Modify your answer to question 4 so that: # o if the lockfile exists, the program will print an error # message and terminate; # o if the lockfile doesn't exist, then the program creates it, # and makes sure that the lockfile was successfully created; # o when the program exits normally, it will remove the lockfile. prog=$(basename $0) uid_num=$(id -u) lockfile="/tmp/${prog}.lock" [ "$uid_num" -eq 0 ] && lockfile=/var/lock/subsys/${prog}.lock if [ -e "$lockfile" ] then echo "lockfile exists" exit 1 else touch $lockfile [ -e $lockfile ] || { echo "$prog: cannot create $lockfile"; exit 1; } fi # just to help in testing: sleep 60 rm -f $lockfile