当年在武汉一家公司实习时,闲暇写的一个通讯录的管理程序。

包含基本的增删查询功能,麻雀虽小,五脏俱全。

这仅仅是一个初稿,大家有兴趣可以修改一下接着用。

#! /bin/bash

#keywords for records
#name,tel,QQ,mail,Address,others

temperary_filename=''

trap 'rm $temperary_filename' EXIT
#using trap command to delete temperary file

name=''
tel=''
QQ=''
mail=''
addr=''
others=''
#varibles for program 
#menu-----user choose options
#tmp -----temperary varibles
menu=''
tmp=''
#current path for this program
current_path=''
option_select() {
echo "          ******please select your choice************"
echo "          ******  a---select to add a new record  *******"
echo "          ******  d---select to delete to a record  *****"
echo "          ******  f---select to find a record  **********"
echo "          ******  r---select to revise a record  ********"
echo "          ******  l---select to list the records  *******"
echo "          ******  q---select to quit!  ******************"
read menu
}
save_information() {
        echo "save information in record file"
        echo "name:$name"                >>record_file.txt
        echo "tel:$tel"                  >>record_file.txt
        echo "QQ:$QQ"                    >>record_file.txt
        echo "address:$addr"     >>record_file.txt
        echo "others:$others"    >>record_file.txt
        echo ""                                  >>record_file.txt
}
records_clear() {
        name=''
        tel=''
        QQ=''
        addr=''
        others=''
}
get_keywords() {
        echo "At least,you should input the name and tel for a record"
        echo "and you can input 'q' to exit after you input the tel "
        echo "press any key to continue"
        read tmp
        echo "please input the name:"
        read name
        echo "please input the tel:"
        read tel
        echo "please input the QQ number:"
        read QQ
        if [ $QQ = "q" ] 
        then 
                QQ=''
                save_information
                records_clear
                return 0
        fi
        echo "please input the address:"
        read addr
    if [ $addr != "q" ]
        then
                addr=''
                save_information #save records information to record_file.txt
                records_clear    #clear the keywords of records
                return 0
        fi
        echo "please input ohter infomation for records"
        read other
        if [ $other != "q" ]
        then
                other=''
                save_information
                records_clear
                return 0
        fi
}
#add a record 
add_record() {
        echo "choose to add a record"
        get_keywords

}
delect_record() {       
        echo "choose to add a record"
        echo "please input the name"
        read tmp
#creating a temperary file,$$ is the pid of this shell program
        #tmp=$(pwd)             #get current shell program file path
        temperary_filename=$$
        #mkdir {tmp}/tempdir
        #echo $temperary_filename
        touch $temperary_filename
        grep -n "^name:$tmp" record_file.txt | cat -n >$temperary_filename
        cat -n  $temperary_filename 
    tmp=`cut -c 6-6 $temperary_filename` #to get the line number
        echo $tmp #print the line numer
#       for i in 0 1 2 3 4 5 
#       do
#               j=$(($tmp+$i))
#               echo $j
#               sed ''$j'd' -i record_file.txt
#       done
    #using piple to show search result
        
        
}
find_record() {
        echo "find a record"
}
revise_record() {
        echo "revise_record"
}
list_records() {
        cat -n record_file.txt | less
}
option_action() {
        case $menu in
                a | A) add_record;;
                d | D) delect_record;;
                f | F) find_record;;
                r | R) revise_record;;
                l | L) list_records;;
                *) echo "no option";;
        esac
}

#now all the functions have been defined and all the varibles have been 
#declared,then it willl go into the 

# before this program runs, check whether the record_file exits or not
# if it does not exists,we create for it
# record_file record the information for records,such as name,tel,QQ,and
# so on
if [ -f record_file ]
then
        echo ""
else
        touch record_file.txt
        echo ""
fi

#add a \n between the command line and proram mune
while [ "$menu" != "q" ]
do
        option_select # select the option 
        option_action # act to the option
#       read menu
done
echo "choice to exit the shell program"
exit 0

 

posted on 2016-02-17 22:50  farbeyond  阅读(1173)  评论(0编辑  收藏  举报