Directory:Derek Elder/Programs/PhonebookSystem

MyWikiBiz, Author Your Legacy — Thursday November 14, 2024
Jump to navigationJump to search
echo "Please enter the name of your phonebook: "
read book
echo "Phone book system"
echo "1) Add an entry"
echo "2) Delete an entry"
echo "3) List file or entry"
echo "4) Change an entry"
echo "5) Exit system"
echo "Please enter your menu choice: "
read choice
while[ $choice != 5 ]
do
  case $choice in
  1)
    addanother=1
    while [ $addanother != 0 ]
    do
      echo "Enter the name to add: "
      read name
      echo "Enter the phone number to add: "
      read number
      echo "The added entry will be $name:$number"
      echo "Is this the entry you wish to add? Enter [Y]es or [N]o"
      read entry
      if [ $entry == "Y" -o $entry == "y" ]
      then
        echo "$name:$number" >> $book
        echo "Add has been performed"
      else
        echo "Add has not been performed"
      fi
      echo "Enter 1 if you would like to add another entry, enter 0 to exit."
      read addanother
    done;;
  2)
    deleteanother=1
    count=0
    while [ $deleteanother != 0 ]
    do
      echo "Enter the name to be deleted: "
      read name
      grep -n $name $book > countbook
      count=`cat countbook | wc -l`
      if [ $count -eq 0 ]
      then
        echo "Error, no matches were found."
      else
        grep -n $name $book
        echo "Which line number would you like to delete?"
        read linum
        sed "$linum d" $book > temp
        temp > $book
      fi
      echo "Enter 1 if you would like to delete another entry, enter 0 to exit."
      read deleteanother
    done;;
  3)
    echo "List choices"
    echo "    1. List Entry"
    echo "    2. List File"
    read menuchoice
    if [ $menuchoice -eq 1 ]
    then
      echo "Enter the name to list: "
      read name
      grep -n $name $book
    else
      echo "Press <enter> to go forward through the file line by line."
      echo "Press <b> to go backward through the file one screen at a time."
      echo "Press <f> to go forward through the file one screen at a time."
      echo "Press <q> when you want to leave the file entry."
      echo "Press <enter> when you want to continue."
      read dummy
      cat $book | less
    fi;;
  4)
    changes=y
    count=0
    while [ $changes == "Y" -o $changes == "y" ]
    do
      echo "Please enter the name you would like to change: "
      read name
      grep -n $name $book
      grep -n $name $book > tempfile
      count=`cat tempfile | wc -l`
      if [ $count -gt 0 ]
      then
        echo "Please enter the line number you would like to change: "
        read linum
        sed -n "$linum p" $book > linumtemp
        echo "Which would you like to change: "
        echo "    1. Name"
        echo "    2. Number"
        read menuchoice
        if [ $menuchoice -eq 1 ]
        then
          oldnumber=`cat linumtemp | cut -d: -f2`
          echo "Please enter the new name: "
          read newname
          sed "$linum d" $book >> tempbook
          mv tempbook $book
          echo "$newname:$oldnumber" >> $book
        else
          oldname=`cat linumtemp | cut -d: -f1`
          echo "Please enter the new number: "
          read newnumber
          sed "$linum d" $book >> tempbook
          mv tempbook $book
          echo "$oldname:$newnumber" >> $book
        fi
      else
        echo "Error, no match found in the phone book."
      fi
      echo "More changes? [Y]es or [N]o"
      read changes
    done;;
  *)
    echo "Error, please enter a valid choice";;
  esac
  echo "1) Add an entry"
  echo "2) Delete an entry"
  echo "3) List file or entry"
  echo "4) Change an entry"
  echo "5) Exit system"
  echo "Please enter your menu choice: "
  read choice
done