All pastes #977321 Raw Edit

Jemand

public text v1 · immutable
·published 2008-04-08 19:25 UTC
rendered paste body
#                                 addrbook1.0
#-------------------------------------------------------------------------- #
# addrbook - just something                                                 #
# Copyright (C) 2008 Christian Schlaeppi                                    #
# ------------------------------------------------------------------------- #
#                                                                           #
# This program is free software; you can redistribute it and/or modify      #
# it under the terms of the GNU General Public License as published by      #
# the Free Software Foundation; either version 2 of the License, or         #
# (at your option) any later version.                                       #
#                                                                           #
# This program is distributed in the hope that it will be useful,           #
# but WITHOUT ANY WARRANTY; without even the implied warranty of            #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
# GNU General Public License for more details.                              #
#                                                                           #
# You should have received a copy of the GNU General Public License         #
# along with this program; if not, write to the Free Software               #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  #
#                                                                           #
# ------------------------------------------------------------------------- #
# mailto:   nekro89@gmail.com                                               #
# ------------------------------------------------------------------------- #
#!/bin/bash

fun_add ()
{
        echo -en "Adding a new contact to the adressbook\n"

        # Getting new contacts name...
        echo -en "Please enter new contact's name\n"
        read cname

        # Checking if Contacts already Exists...

        if [ -e "$cname" ] ;
        then
                echo -en "The Contact you want to add already exists\n"
                echo -en "Do you want to change that contact?(Yes/No)\n"
                read cchange

                # Changeing contact or not changing contact
                case $cchange in
                        Y* | y* ) fun_change ;;
                        N* | n* )
                                echo -en "Okay Exiting...\n"
                                exit 0 ;;
                esac

        else

                # Getting new contact's adress...
                echo -en "Please enter new contact's adress\n"
                read caddr

                # Getting new contact's zip and city...
                echo -en "Please enter new contact's city and zip-code\n"
                read czip

                # Getting new contact's fon number...
                echo -en "Please enter new contact's phone number\n"
                read cfon

                # Getting new contact's mobile number...
                echo -en "Please enter new contact's mobile number\n"
                read cmobile

                # Getting new contact's mail addr...
                echo -en "Please enter new contact E-Mail adress\n"
                read cmail
                echo -en "\n\n"

                # Creating new contact's file in directory book
                touch book/"$cname"


                # Writing contact's information

                echo -en "-------------------------------------\n" >> book/"$cname"
                echo -en "Name:\t$cname\n" >> book/"$cname"
                echo -en "Adress:\t$caddr\n" >> book/"$cname"
                echo -en "Zip/City:\t$czip\n" >> book/"$cname"
                echo -en "Phone #:\t$cfon\n" >> book/"$cname"
                echo -en "Mobile #:\t$cmobile\n" >> book/"$cname"
                echo -en "E-Mail:\t$cmail\n" >> book/"$cname"
                echo -en "-------------------------------------\n" >> book/"$cname"

                cat book/"$cname"

                echo -en "Job done, new contact has been added. Have a nice day!\n"
                exit 0


        fi
}




fun_search ()
{
        # Getting Searchable String
        echo -en "Type the contact you want to search in your adressbook (Case sensetive!)\n"
        echo -en "Lists all Contacts which matches your entry\n"
        read cfind

        # Grep for the searched item
        grep "$cfind" book/* > /dev/null 2>&1

        # Check Item
        if [ "$?" == "0" ] ;
        then
                # Item exists...
                echo -en "Contact "$cfind" is an existing item of your adressbook:\n\n"
                cat book/*"$cfind"*

        else
                # Item doesn't exist...
                echo -en "Contact "$cfind" is not an existing item of your addressbook\n"
                echo -en "Do you want to add this contact now? (Yes/No)\n"
                read cadd

                # Add or No add - Thats the question here ;)
                case $cadd in
                        Y* | y* ) fun_add ;;
                        N* | n* ) echo -en "Okay. Exiting...\n"
                esac
        fi


}

fun_change ()
{
        # Changing a contanct...
        echo -en "Type Contact you want to change (Case sensetive)\n"
        read schange

        # Checking if contact exists

        if [ -e book/"$schange" ] ;
        then

                echo -en "Which Atribute do you want to change?\n"
                echo
                echo -en "1 = Name\n"
                echo -en "2 = Adress\n"
                echo -en "3 = Zip Code and City\n"
                echo -en "4 = Phone number\n"
                echo -en "5 = Mobile Number\n"
                echo -en "6 = E-Mail Adress\n"
                echo -en "7 = Menu\n"
                echo
                cat book/*"$schange"
                read catribute

                case $catribute in

                        1 ) # Changeing Name

                                echo -en "Changing Name\n"

                                oldname=$(grep "Name" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new Name\n"
                                read newname

                                mv book/"$schange" book/"$newname"

                                sed -i "s/$oldname/\t$newname/g" book/"$newname"
                                cat book/"$newname" ;;

                        2 )
                                echo -en "Changing Adress\n"

                                oldadress=$(grep "Adress" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new Adress\n"
                                read newaddr

                                sed -i "s/$oldadress/\t$newaddr/g" book/"$schange"
                                cat book/"$schange" ;;

                        3 )
                                echo -en "Changing Zip/City\n"

                                oldcit=$(grep "Zip/City" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new Zip/City\n"
                                read newcit

                                sed -i "s/$oldcit/\t$newcit/g" book/"$schange"
                                cat book/"$schange" ;;

                        4 )
                                echo -en "Changing Phonenumber\n"

                                oldnum=$(grep "Phone" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new Phonenumber\n"
                                read newnum

                                sed -i "s/$oldnum/\t$newnum/g" book/"$schange"
                                cat book/"$schange" ;;

                        5 )
                                echo -en "Changing Mobilenumber\n"

                                oldmobnum=$(grep "Mobile" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new Mobilenumber\n"
                                read newmobnum

                                sed -i "s/$oldmobnum/\t$newmobnum/g" book/"$schange"
                                cat book/"$schange" ;;

                        6 )
                                echo -en "Changing E-Mail Adress\n"

                                oldmail=$(grep "E-Mail" book/"$schange" | awk -F':' '{print $2}')

                                echo -en "Type new E-Mail Adress\n"
                                read newmail

                                sed -i "s/$oldmail/\t$newmail/g" book/"$schange"
                                cat book/"$schange" ;;

                        7 )
                                fun_begin ;;

                esac


        else
                # No contact found to change, may be added
                echo -en "contact does not exist, do you want to add?(Yes/No)\n"
                read add

                case $add in
                        Y* | y* ) fun_add ;;
                        N* | n* ) echo -en "Okay, Exiting...\n"
                                exit 0 ;;
                esac
        fi


}

fun_begin ()
{

                # Find out what user wants

                echo -en "Type 'A' to add a contact\n"
                echo -en "Type 'S' to Search a contact\n"
                echo -en "Type 'ls' to list all contacts\n"
                echo -en "Type 'ch' to change an contact\n"
                echo -en "Type 'q' to quit\n"
                read doing

                # Finding out what the dude wants to...
                case $doing in
                        A | a ) fun_add ;;
                        S | s ) fun_search ;;
                        ls ) fun_lsal ;;
                        ch ) fun_change;;
                        Q | q ) echo -en "Okay, exiting...\n"
                                exit 0 ;;
                        * ) echo -en "No such function found, please try again\n"
                            sleep 2
                            clear
                            fun_begin ;;

                esac
}

fun_lsal ()
{
                # listing all contact and checking id it is too much

                amo=$(ls -lah book/* | wc -l)

                        if [ "$amo" -ge "20" ] ;
                        then
                                # If there are more than 20 entries...
                                echo -en "There are more then 20 entries in your book. Show them anyway?(Yes/No)\n"
                                read show
                                        case $show in
                                                Y* | y* ) cat book/* | more
                                                          echo -en "$amo Contacts in your addrbook\n" ;;
                                                N* | n* ) echo -en "Okay, exiting...\n"
                                                          exit 0 ;;
                                        esac
                        else
                                # if there are less then 20 entries...
                                cat book/*
                                echo -en "$amo Contacts in your addrbook\n"

                        fi
}

#########################################################################################
#                               Starting the whole stuff...                             #
#########################################################################################
clear
echo -en "Welcome to the local Adressbook Version 1.1\n"

# Testing for directory book

ls -lah book/ > /dev/null 2>&1

if [ "$?" == "0" ] ;
then
        fun_begin
else
        mkdir book
        fun_begin
fi