Download this file
#!/bin/ksh
# didit2 - Removes items from your $HOME/.todo list and adds the removed
# item to your $HOME/.done file. (NOTE: This version includes many extra
# features, intended to spark your imagination. None of these extra features
# were required for the Project!)
# Written by Wayne Pollock, Tampa Florida 1997
#set -x # uncomment this line to debug
# Security section:
PATH='/bin' # Reset PATH
umask 077 # Set mode of created (temproary) files to 600
unset ENV # Don't import any new aliases
for a in $(alias |sed 's/=.*//') # Remove all current ksh aliases
do unalias $a
done
# End of Security section
# Define short, easy-to-remember names for the files used:
TODO=$HOME/.todo # The user's list of things to do
DONE=$HOME/.done # The list of accomplished stuff
TempFile=/tmp/todo.$$ # The temporary file to hold the sed output
RC=-1 # The Return Code (Exit Status) to use
# when exiting "didit" due to errors.
# Validation section:
# Check the TODO file exists and is readable with
# at least one item in the list, and that the user provided exactly one
# argument, and that arguement is a number between 1 and NUM_ITEMS.
if [ ! -s $TODO ]
then
echo "### Error:"
echo " $0 can't remove item #$1 since todo list is empty!"
echo
exit $RC # Quit and return an error status
fi
# Count the number of items in the TODO file:
NUM_ITEMS=$(wc -l < $TODO)
OK=TRUE
# Check that only one argument was supplied:
if [ $# -ne 1 ]
then
OK=FALSE # No good, either zero or more than one arguments!
fi
# Check that argument is a number:
case "$1" in
+([0-9])) ;; # $1 is a number, all is well
*) OK=FALSE # Darn! $1 is not a number, so set OK to FALSE
set -- 0 ;; # Reset $1 to a number (zero) so the following test
esac # doesn't crash.
if [ "$OK" = "FALSE" -o "$1" -le 0 -o "$1" -gt $NUM_ITEMS ]
then
echo "### Error: type '$0 ' to remove item from todo list"
echo
exit $RC # Quit and return an error status
fi
# End of validation section
# When the script is done, or on reciept of a signal, remove TempFile and
# exit with an exit status of $RC:
trap '/bin/rm -f $TempFile; trap 0; exit $RC' 0 1 2 3 15
# Append the selected line from TODO to DONE, then delete from TODO:
sed -e "${1}w $DONE" -e "${1}d" $TODO >$TempFile 2>/dev/null
if [ $? = 0 ]
then mv $TempFile $TODO # sed worked, so replace the TODO file
RC=$?
else echo "### Error: sed failed (contact Tech support!)"
fi
# Done! Script returns the exit status of "$RC", which is set to -1 on error.