自己编的一个通讯录系统(简陋了点)

发表于:2007-07-04来源:作者:点击数: 标签:
为了复习一下自学的shell编程,闲余时间编了个通讯录,主要有三个主体部分,本来想加个编辑功能的,想了想,里面内容这么少,要编辑不如将原来的覆盖不就OK了嘛,简单一个功能却要加几十行,弄得这么麻烦,算了,就下面的了^_^ 主函数脚本: #!/bin/sh sourc

为了复习一下自学的shell编程,闲余时间编了个通讯录,主要有三个主体部分,本来想加个编辑功能的,想了想,里面内容这么少,要编辑不如将原来的覆盖不就OK了嘛,简单一个功能却要加几十行,弄得这么麻烦,算了,就下面的了^_^
主函数脚本:
#!/bin/sh
                                                                                                                            
source add
source show
source delete
                                                                                                                            
function menu()
{
echo -e "\33[34m"
cat << EOF
        What you want to do? Please input you choice:
 
                [1] Add a person into the address book!
                [2] Check out a person's data in the address book!
                [3] Delete a person from the origin address book!
                [4] Quit without do anything!
 
EOF
echo -ne "Please select [ 1, 2, 3, or 4 ]:\33[0m"
 
while read choice
        do
                case $choice in
                        1 ) add;;
                        2 ) show ;;
                        3 ) delete ;;
                        4 ) echo ByeBye!; exit 0 ;;
                        * ) echo -ne "\33[31mError! Please input [ 1, 2, 3, or 4 ]:  \33[0m";;
                esac
done
}
 
echo -e "\33[32m\t\tWellcome to My addressbook!\33[0m"
menu

 chauney 回复于:2005-03-18 21:03:14
function checkperson ()
{
        if [ -z "$name" ]; then
                echo -e "\33[31mError! the Name is incorrect!\n Please Input the person's name and other messages again: \33[0m"
                addperson
        fi
        if [ -f "addressbook" ]
                then
                        showname
                        if [ ! -z "$showname" ] ; then
                                echo  "The name you input has already exist! Do you want to delete the origin name? [ y or n or q ( quit to main menu )]"
                                while read
                                do
                                        case $REPLY in
                                                y|Y|[Yy][Ee][Ss] )
                                                        awk -F: -v new=$name '{ if ( $1 != new ) print $0; }' addressbook  > /tmp/tmp$$
                                                        mv -f /tmp/tmp$$ addressbook 2> /dev/null
                                                        check
                                                        identify
                                                ;;
                                                n|N|[Nn][Oo] )
                                                        echo Please input the name again:
                                                        read name
                                                        checkperson
                                                        check
                                                        identify
                                                ;;
                                                q|Q )
                                                        menu
                                                ;;
                                                * )
                                                        echo  "Error! Please input y or n or q (quit to main menu )!"
                                                ;;
                                        esac
                                done
                        fi
        fi
}
 
function checkmail ()
{
        if [ -z $mail ]; then mail="user@localhost.localdomain"; fi
        mail_check=`echo $mail | sed -n '/\@*\./p'`
        if [ -z $mail_check ]
                then
                        echo -ne "The E-mail you input is wrong!\nPlease input again: "
                        read  mail
                        checkmail
        fi
}
 
function checkphone ()
{
        if [ -z "$phone" ]; then phone=0; fi
        if [ -z "$mobile" ]; then mobile=0; fi
        phone_check=`echo $phone | tr ':;",.+<>?/~\!=@#$%^&*[a-zA-Z]{}|' ' ' `
        mobile_check=`echo $mobile | tr ':;",.+<>?/~\!=@#$%^&*[a-zA-Z]{}|' ' ' `
        if [ "$phone_check" != "$phone" ]
                then
                        echo -ne "The Phone Number you input is incorrect!\nPlease input again: "
                        read phone
                        checkphone
        elif [ "$mobile_check" != "$mobile" ]
                then
                        echo -ne "The Mobile Phone Number you input is incorrect!\nPlease input again: "
                        read mobile
                        checkphone
        fi
}
 
function redo ()
{
        select pick in "Name" "E-mail" "Address" "Phone Number" "Moblie Phone Number" "Other Messages" "Exit to the identify menu" "Exit to the main menu" "Quit without save"
                do
                case $pick in
                        Name )
                                echo Please input the name again:
                                read name
                                checkperson
                                redo
                        ;;
                        E-mail )
                                echo Please input the E-mail again:
                                read mail
                                checkmail
                                redo
                        ;;
                        Address )
                                echo Please input the address again:
                                read address
                                redo
                        ;;
                        "Phone Number" )
                                echo Please input the phone number again:
                                read phone
                                checkphone
                                redo
                        ;;
                        "Moblie Phone Number" )
                                echo Please input the mobile phone number again:
                                read mobile
                                checkphone
                                redo
                        ;;
                        "Other Messages" )
                                echo Please input the other messages again:
                                read message
                                redo
                        ;;
                        "Exit to the main menu" )
                                menu
                        ;;
                        "Exit to the identify menu" )
                                identify
                        ;;
                        "Quit without save" )
                                exit 0
                        ;;
                        * )     echo -e "This is not a valid choice. Try again:"
                                pick=
                        ;;
                esac
        done
}
 
function check ()
{
        checkperson
        checkmail
        checkphone
        if [ -z "$address" ]; then address="Null"; fi
        if [ -z "$message" ]; then message="Null"; fi
}
 
function identify ()
{
        cat << DATA
        The data you input is on the next:
 
                Name: $name
                E-mail: $mail
                Address: $address
                Phone Number: $phone
                Mobile Phone Number: $mobile
                Other Message: $message
 
        Is it right?[ y or n ]:
DATA
        while read answer
        do
                case $answer in
                        y|Y|[Yy][Ee][Ss] )
                                        echo $name:$mail:$address:$phone:$mobile:$message >> addressbook ;
                                        echo Do you want to add another person [ y or n ,and any other key will exit! ]:
                                        read
                                        case $REPLY in
                                                y|Y|[Yy][Ee][Ss] ) add;;
                                                n|N|[Nn][Oo] ) menu;;
                                                * ) exit 0;;
                                        esac
                                        ;;
                        n|N|[Nn][Oo] )  echo Which one is incorrect? Pick out it :; redo;;
                        * ) echo -ne "\33[31mError! Please input [ y or n ]:  \33[0m"
                esac
done
}
 
function addperson ()
{
        echo -n "Input the Name: "; read name ;
        echo -n "Input the E-mail: "; read mail ;
        echo -n "Input the Address: "; read address ;
        echo -n "Input the Phone Number: "; read phone ;
        echo -n "Input the Mobile Phone Number: "; read mobile ;
        echo -n "Input the other messages if necessary: "; read message ;
        check;
        identify;
}
 
function add ()
{
                echo Please  follow the next steps !
                stty erase '^?'
                addperson
}

 chauney 回复于:2005-03-18 21:04:48
show脚本,用来查询的:
function answer ()
{
                read
                case $REPLY in
                        y|Y|[Yy][Ee][Ss] ) show ;;
                        n|N|[Nn][Oo] ) menu ;;
                        * ) exit 0;;
                esac
}
 
function showname ()
{
        showname=`awk -F: -v new=$name '{ if ( $1 == new ) print $0; }' addressbook | awk -F: '{ printf "%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n\n", "Name: ", $1, "E-mail: ",$2,  "Address: ", $3,  "Phone Number: ", $4,  "Moblie Phone Number: ", $5, "Other Messages: ", $6 ; }' `
}
 
function showall ()
{
        awk -F: '{ printf "%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n\n", "Name: ", $1, "E-mail: ",$2,  "Address: ", $3,  "Phone Number: ", $4,  "Moblie Phone Number: ", $5, "Other Messages: ", $6 ; }' addressbook
}
 
function show ()
{
        echo -ne "\33[33mWhose data  you want to know ?  \n\33[34mType his or her name directly ( Type "all" for check out for all people! ):  \33[0m"
        read name
        if [ -f "addressbook" ]
                then
                        if [ $name == "all" ]
                                then
                                        showall
                                        echo -e "\33[33mType "y" to check a person's data, and "n" to the main menu, other keys will exit!\33[0m"
                                        answer
                        else
                                        showname
                                        if [ -z "$showname" ]; then
                                                echo -e "The person you want to know does not exist! \nDo you want to try again or check out other's data? [y or n ,and any other key will exit! ]"
                                                answer
                                        else
                                                echo -e "$showname"
                                                echo Do you want to check another person\'s data? [y or n ,and any other key will exit! ]
                                                answer
                                        fi
                        fi
        else echo -e "\33[31mThe addressbook isn't exist!\33[0m"; menu
        fi
}

 chauney 回复于:2005-03-18 21:05:59
用来删除内容的:
function deldata ()
{
        showname
        if [  -z  "$showname" ]; then
                echo  -e "\33[31mThe person you want to delete does not exist! Please check the name first!:\33[0m"
                menu
        else
                echo -e "The person 's data is :\n$showname"
                echo -ne "\33[34m\tIs it right? \33[0m[ y or n ]: "
                read
                if [[ "$REPLY" == [Yy] ]]; then
                        awk -F: -v new=$name '{ if ( $1 != new ) print $0; }' addressbook  > /tmp/tmp$$
                        mv -f /tmp/tmp$$ addressbook 2> /dev/null
                        if [ $? == 0 ]; then
                                echo The data  is updated suclearcase/" target="_blank" >ccessfully!
                                menu
                        else
                                echo There are something unexpected happen! Please try again!
                                delete
                        fi
                else
                        echo -e "\33[31m\t\tSkip to the main menu\33[0m"
                        menu
                fi
        fi
}
 
function delete ()
{
        echo -n "Type a name you want to delete:  "
        read name
        deldata
}

 yenakata 回复于:2005-03-18 21:08:33
非常感谢,收下了学学,最近在学这方面.

 chauney 回复于:2005-03-18 21:09:32
我编的这些有很多地方感觉很亢余,不过以后再改吧,没啥子时间最近^_^

 NTT2 回复于:2005-03-18 21:14:28
已经很不错了,帮顶!!!

 hawkli 回复于:2005-03-18 22:20:35
不错的东西。

 doublezha8 回复于:2005-03-19 09:01:47
长了见识!

 flighttop 回复于:2005-03-20 04:47:52
I am not familiar with shell programming. I cannot run your program on my Linux box.

 vvwj12wu 回复于:2005-03-20 09:40:08
楼主厉害哦!
还希望多多指教!
我不会shell编程!只会一点点perl!

 wxijin 回复于:2005-03-21 09:51:33


 chauney 回复于:2005-03-21 13:48:20
你应该分别保存到不同的文件:main,add,show,delete,再赋予main可执行属性,应该不会有问题的,我用的都是常用命令

 1212everyday 回复于:2005-03-21 16:08:39
不错
收着看看

 Zelgadiss 回复于:2005-03-22 09:44:00
./main: add: line 187: syntax error: unexpected end of file
./main: line 36: syntax error: unexpected end of file

-rw-rw-r--  1 wub wub 7690 Mar 22 09:45 add
-rw-rw-r--  1 wub wub 1237 Mar 22 09:44 delete
-rwxrwxr-x  1 wub wub 1133 Mar 22 09:48 main
-rw-rw-r--  1 wub wub 2273 Mar 22 09:43 show

 shenlinshan999999999 回复于:2005-03-24 15:57:04
真长知识呀!谢谢分享!

 chauney 回复于:2005-03-27 20:52:24
我修改后的程序发在了shell板块:
http://bbs.chinaunix.net/forum/viewtopic.php?t=516169&highlight=chauney
有兴趣的话去看看吧^_^

原文转自:http://www.ltesting.net