Advertisement
solidsnake

OS Address Book Exercise (With functions)

Jan 28th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #Author - MR
  2. #Date modified - 2/12/14
  3.  
  4. echo "Rosales,Clifford,123456,crosales@guidon.com" > addressbook
  5. echo "Hizon,Joseph,654321,jhizon@batak.com" >> addressbook
  6. echo "Raymundo,Miguel,789012,mraymundo@gmail.com" >>addressbook
  7.  
  8. until [ "$choice" == "4" ] #watch out for rookie mistakes
  9.     do
  10.         echo "======================"
  11.         echo "What do you want to do?"
  12.         echo "======================"
  13.         echo "1. Add new record"
  14.         echo "2. Display all records"
  15.         echo "3. Search"
  16.         echo "4. Exit"
  17.         echo "======================"
  18.         echo -n "Choice: "
  19.         read choice
  20.         echo
  21.        
  22.         # string -> "$x" == "y" --- int -> $x -eq y
  23.        
  24.         add_contact()
  25.         {
  26.             echo -n "Enter last name: "
  27.             read last
  28.             echo -n "Enter first name: "
  29.             read first
  30.             echo -n "Enter contact number: "
  31.             read number
  32.             echo -n "Enter email address: "
  33.             read email
  34.             echo "$last,$first,$number,$email" >> addressbook
  35.             echo
  36.             echo "~~~CONTACT SUCCESSFULLY ADDED~~~"
  37.             echo
  38.         }
  39.            
  40.        
  41.         display_contacts()
  42.         {
  43.             cat addressbook | sed 's/,/ /g' | awk '{printf "%-15s%-15s%-15s%-15s\n",$1,$2,$3,$4}'
  44.             echo
  45.         }
  46.            
  47.        
  48.        
  49.         search()
  50.         {
  51.             echo -n "Entery query: "
  52.             read query
  53.             echo
  54.             echo "SEARCH RESULTS:"
  55.             echo
  56.             cat addressbook | grep -i $query
  57.             echo
  58.         }
  59.            
  60.        
  61.         invalid()
  62.         {
  63.             echo “INVALID CHOICE”
  64.             echo
  65.         }
  66.            
  67.         if [ $choice == "1" ]
  68.         then
  69.             add_contact
  70.         elif [ $choice == "2" ]
  71.         then
  72.             display_contacts
  73.         elif [ $choice == "3" ]
  74.         then
  75.             search
  76.         elif [ $choice == "4" ]
  77.         then
  78.             echo "Auf wiedersehen"
  79.             echo
  80.         else
  81.             invalid
  82.         fi
  83.        
  84.     done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement