Register
It is currently Sun Dec 21, 2014 4:14 am

Phone Book Script - A very quick phone book written in bash.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 9 posts ] 
Author Message
 PostPosted: Wed Aug 08, 2007 2:51 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Phone Book Script - A very quick phone book written in bash.

wget http://crouse.us/scripts/bash/phonebook.sh

Code:
#!/bin/bash
#    Phone Book Script - A very quick phone book written in bash.
#    FILE: phonebook.sh
#    VERSION: 0.2
#    DATE: August 7, 2007
#    AUTHOR: Dave Crouse - Please visit bashscripts.org and usalug.org

# Initialize some files
mkdir -p ~/.baps; touch ~/.baps/phonebook.data;
touch ~/.baps/temp.data
cat /dev/null > ~/.baps/temp.data

# The phonebook functions
headerfile ()
{
clear;
echo "*********************************************";
echo "*         Crouse's Bash PhoneBook           *";
echo "*********************************************";
echo " ";
echo "   The results returned for a search of: $searchforthis" ;
echo "     ______________________________________";
echo " "
sort -d ~/.baps/temp.data | nl -ba ;
echo "     ______________________________________";
echo "";
}

mainmenu ()
{
menu="     s)earch a)dd  r)emove  q)uit"
while true; do

headerfile;
echo -e "$menu"
echo "";
read -p "Please choose one of the options above : " option

case $option in
      s)   searchrecords; ;;
      a)   addrecord; ;;
      r)   removerecord; ;;   
      q) exit; ;;
   #Unlisted Options are below
       print) sort -d ~/.baps/phonebook.data | nl -ba | lpr ; ;;
       list)  listallrecords; ;;
      alias)  echo "alias phonebook='sh $PWD/phonebook.sh'" >> ~/.bashrc; ;;
      clear) rm ~/.baps/temp.data; touch ~/.baps/temp.data;;
#Uncomment the line below if you want the option of erasing the phone book.
# erase) erasephonebook ;;
      *)   echo "Sorry, that isn't an option, try again. "; sleep 2;  ;;
   esac
done;
}

addrecord ()
{
clear;
echo "*********************************************";
echo "*         Crouse's Bash PhoneBook           *";
echo "*********************************************";
read -p "ADDING RECORD: Enter Name " name;
read -p "ADDING RECORD: Enter Phone Number " number;
entry="$name $number"
echo $entry >> ~/.baps/phonebook.data
echo "Record Added: $entry"; sleep 1;

}

searchrecords ()
{
cat /dev/null > ~/.baps/temp.data
read -p "Search for : " searchforthis
if [[ $searchforthis != "" ]]
      then
   grep -i "$searchforthis" ~/.baps/phonebook.data > ~/.baps/temp.data
   headerfile
      else
        echo ""
   echo "Sorry, you must enter something to search for!"
   read -p "Hit any key to continue. " temp
fi
}

erasephonebook ()
{
read -p "$USER are you sure you want to erase the entire phone directory ? y/n: " reallycontinue
if [ "$reallycontinue" = "y" ]
   then
   cp ~/.baps/phonebook.data ~/.baps/phonebook.data~~
   rm ~/.baps/phonebook.data; echo "!! Erasing Entire Directory !!" ; touch ~/.baps/phonebook.data; sleep 1;
   rm ~/.baps/temp.data; touch ~/.baps/temp.data;
fi
}

removerecord ()
{
touch ~/.baps/temp2.data; cat /dev/null > ~/.baps/temp2.data;
sort -d ~/.baps/phonebook.data > ~/.baps/temp2.data
mv ~/.baps/temp2.data  ~/.baps/phonebook.data
clear;
echo "*********************************************";
echo "*         Crouse's Bash PhoneBook           *";
echo "*********************************************";
echo "    ~~~~~~~~ Removing Record ~~~~~~~~";
echo " ";
echo "     ______________________________________";
echo " Line Number   | Record";
nl -ba ~/.baps/phonebook.data
echo "     ______________________________________";
echo " ";
echo "    ~~~~~~~~ Removing Record ~~~~~~~~";
     read -p "Enter the line number of the record you would like to delete : " removeline;
     if [[ $removeline != "" ]]
     then
         cat /dev/null > ~/.baps/temp2.data;
         cat ~/.baps/phonebook.data | sed ''$removeline'd' > ~/.baps/temp2.data;
         mv ~/.baps/temp2.data ~/.baps/phonebook.data
    echo "Record Removed!" ; sleep 1;
     fi
}

listallrecords ()
{
clear; sort -d ~/.baps/phonebook.data | nl -ba | less ;
}

# Program run starts here #
mainmenu
exit 0


Top
 Profile WWW  
 PostPosted: Wed Dec 02, 2009 1:57 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Hi mate handy script, is it possible to add an edit record function?


Top
 Profile  
 PostPosted: Thu Dec 03, 2009 3:33 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
I'm sure it is ;) TBH, I normally just opened up the text file and edited it if I had an edit.
emacs
CTL+s to search and edit is pretty fast ;)

I have thought about rewriting this for a year or two, I may get around to it one day........... or you could "fix" it for me if you want :D :D


Top
 Profile WWW  
 PostPosted: Fri Dec 04, 2009 2:14 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
I just tried to add edit function and just manged to delete all entries DOH!


Top
 Profile  
 PostPosted: Tue Dec 08, 2009 8:50 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
=)) Perhaps a "backup phonebook" function is needed as well ;) :D :D

It wouldn't be humourous to me if I hadn't deleted data before myself ....... don't feel like the lone ranger.
I may look into this script again, as it was written soooo long ago, I can think of tons of things I'd do differently and add in.... might be a good snowday project ;)


Top
 Profile WWW  
 PostPosted: Fri Oct 15, 2010 8:49 am   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
Hi Crouse any chance your going to resurrect this as i'm looking at using it again?


Top
 Profile  
 PostPosted: Wed Oct 20, 2010 2:21 pm   

Joined: Fri May 16, 2008 4:58 am
Posts: 94
would be nice to have results displayed under columns ie

name | phone number | etc etc |


Top
 Profile  
 PostPosted: Mon Nov 22, 2010 4:08 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
You can check my newest phonebook script here:
viewtopic.php?f=7&t=1252


Top
 Profile WWW  
 PostPosted: Fri Feb 25, 2011 2:29 pm   
BANNED

Joined: Thu Feb 24, 2011 12:10 am
Posts: 1
USER BANNED
I hate spammers ~~ CROUSE


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP