Register
It is currently Sat Oct 25, 2014 3:29 am

Phone Book Script - Version 2


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Nov 22, 2010 4:07 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Phone Book Script - Version 2
This version uses sqlite3 and bash.
Still has things i could fix...but it works.

Code:
#!/bin/bash
# Crouse's sqlite+bash phonebook application.
# 11-22-2010 Crouse

# Check if requirements are installed.
clear
if [[ -z $( type -p sqlite3 ) ]]; then echo -e "REQUIRED: sqlite3 -- NOT INSTALLED !";exit ;fi

# Check if database already exists, if not, create it.
database="/home/$USER/.phonebook.db"
if [ -e "$database" ];then
     echo ""
else
     echo "Datase does not exist - Creating now"
sqlite3 $database "create table phonebook (id INTEGER PRIMARY KEY,First TEXT,Last TEXT,Street TEXT,City TEXT,State TEXT,Zip TEXT,Phone TEXT,Email TEXT, Notes);"
    echo "Database created at $database"
fi

# Check options given to the program and "do stuff".
case ${1} in

# oops--you didn't give me anything
"") echo "Crouse's sqlite+bash phonebook"
    echo "================================"
    echo "Usage: ${0##*/} <option>";
    echo "";
    echo "  --------------------"   
    echo " Create an entry"
    echo "   $0 -c"
    echo "  --------------------"
    echo " Search by last name \"-l";
    echo "       -l Smith";
    echo "";
    echo " Search by phone number";
    echo "       -p 515-555-5555";
    echo ""
    echo " Search by email address";
    echo "       -em xxxx@usalug.net";
    echo ""
    echo " Search by first name with no flags";
    echo "   $0 dave";
    echo "  --------------------"
    echo " Edit an entry"
    echo "   $0 -e"
    echo "================================"
    echo ""
;;

# Create an entry
"-c")
clear
echo "Phone Book - Insert data into database"
echo "--------------------------------------"
echo "FORMAT: First Name,Last Name,Street,City,State,Zip,Phone,Email,Notes"
echo " "

#Insert data into database
read -p "First Name: " First
read -p " Last Name: " Last
read -p "    Street: " Street
read -p "      City: " City
read -p "     State: " State
read -p "       Zip: " Zip
read -p "     Phone: " Phone
read -p "     Email: " Email
read -p "     Notes: " Notes

sqlite3 $database "insert into phonebook (First,Last,Street,City,State,Zip,Phone,Email,Notes) values ('$First','$Last','$Street','$City','$State','$Zip','$Phone','$Email','$Notes');"

#clear
echo "-----------------------------"
echo "Data Entered into Database"
;;


# Edit an entry
"-e")
clear
read -p  "Enter id# to edit: " idnum
echo ""

sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook WHERE id = '${idnum}';
.quit
EOF

echo ""
echo "Hitting enter will just leave that line as is."
echo ""
echo "-----------------------------------------------"
#Insert data into database
read -p "First Name: " First
read -p " Last Name: " Last
read -p "    Street: " Street
read -p "      City: " City
read -p "     State: " State
read -p "       Zip: " Zip
read -p "     Phone: " Phone
read -p "     Email: " Email
read -p "     Notes: " Notes

echo ""

if [[ "$First" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET First = '${First}' WHERE id='${idnum}';"
fi

if [[ "$Last" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Last = '${Last}' WHERE id='${idnum}';"
fi

if [[ "$Street" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Street = '${Street}' WHERE id='${idnum}';"
fi

if [[ "$City" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET City = '${City}' WHERE id='${idnum}';"
fi

if [[ "$State" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET State = '${State}' WHERE id='${idnum}';"
fi

if [[ "$Zip" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Zip = '${Zip}' WHERE id='${idnum}';"
fi

if [[ "$Phone" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Phone = '${Phone}' WHERE id='${idnum}';"
fi

if [[ "$Email" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Email = '${Email}' WHERE id='${idnum}';"
fi

if [[ "$Notes" != "" ]]; then
  sqlite3 $database "UPDATE phonebook SET Notes = '${Notes}' WHERE id='${idnum}';"
fi
;;



# Search by last name
"-l")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook WHERE Last LIKE '%${2}%';
.quit
EOF
;;

# Search by phone
"-p")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook WHERE Phone LIKE '%${2}%';
.quit
EOF
;;

# Search by email
"-em")
echo ""
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook WHERE Email LIKE '%${2}%';
.quit
EOF
;;

# Search by first name for all others
*)
echo "";
sqlite3 $database<<EOF
.header off
.mode line
select id,First,Last,Street,City,State,Zip,Phone,Email,Notes from phonebook WHERE First LIKE '%${1}%';
.quit
EOF
;;

esac
echo ""
exit 0



Code:
Crouse's sqlite+bash phonebook
================================
Usage: pb <option>

  --------------------
Create an entry
   /usr/local/bin/pb -c
  --------------------
Search by last name "-l
       -l Smith

Search by phone number
       -p 515-555-5555

Search by email address
       -em xxxxx@usalug.net

Search by first name with no flags
   /usr/local/bin/pb dave
  --------------------
Edit an entry
   /usr/local/bin/pb -e
================================


Top
 Profile WWW  
 PostPosted: Mon Dec 20, 2010 12:24 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
*Jeo wonders how hard it would be to use the google 'portable contacts api' to import google contacts into crouse's address book... or use the full 'contacts data api' to sync them* :)


Top
 Profile YIM  
 PostPosted: Wed Dec 22, 2010 4:51 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
I'm not even sure what that api looks like ;)

Interesting idea though .... now you have me thinking... lol.

Good to see you here again jeo :)


Top
 Profile WWW  
 PostPosted: Thu Dec 23, 2010 9:27 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Glad to be back! I *started* looking at the google api stuff the other day, but haven't gotten very deep into it yet. I think I've finally decided to use Google as my central contact list, but having it all be online, on a system that's beyond my control makes me nervous, so off-line copies make me happy :)


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


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:  


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