Shell Programming Case Study #8


#!/bin/sh
# case study #8
# using the case statement to implement a menu

systembackup() {
   BLOG=/backuplog
   TAPE=/dev/rmt0
   (
      echo "Are you sure you want to backup now? \c" > /dev/tty
      read ANS
      case "$ANS" in
         [Nn]*) exit;;
         [Yy]*) ;;   # just fall through if yes
         *) 
            echo "Invalid input. Program aborted" > /dev/tty
               # 1st echo is for the screen
            echo "Invalid input. Program aborted `date`" 
               # 2nd echo is for log
            exit 1
            ;;
      esac
      echo "start backup `date`" 
      cd /
      find . | cpio -oc > $TAPE
      STATUS=$?
      echo "end backup with status $STATUS `date`" 
   ) >> $BLOG 2>&1
   # 2>&1 saves find and cpio errors in BLOG also
}

echo "
Menu
   1 show the date
   2 show the current dir
   3 list the current dir
   4 edit a file
   5 backup the system

Enter your choice: \c"

read CHOICE

case "$CHOICE" in
   1) date ;;
   2) pwd  ;;
   3) ls ;;
   4) echo "Enter filename to edit: \c"
      read FILE
      if [ ! -r "$FILE" ]
      then
         echo "Error: cannot access $FILE" >&2
         exit 1
      fi
      vi $FILE
      ;;
   5) systembackup;;
   *) echo "Invalid option" >&2 ;;
esac