免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4925 | 回复: 16
打印 上一主题 下一主题

自己编的一个通讯录系统(简陋了点) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-18 21:01 |只看该作者 |倒序浏览
为了复习一下自学的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 "lease 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

论坛徽章:
0
2 [报告]
发表于 2005-03-18 21:03 |只看该作者

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

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" "hone 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
                        ;;
                        "hone 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 $namemailaddressphonemobilemessage >;>; 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
}

论坛徽章:
0
3 [报告]
发表于 2005-03-18 21:04 |只看该作者

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

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,  "hone 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,  "hone 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
}

论坛徽章:
0
4 [报告]
发表于 2005-03-18 21:05 |只看该作者

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

用来删除内容的:
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 successfully!
                                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
}

论坛徽章:
0
5 [报告]
发表于 2005-03-18 21:08 |只看该作者

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

非常感谢,收下了学学,最近在学这方面.

论坛徽章:
0
6 [报告]
发表于 2005-03-18 21:09 |只看该作者

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

我编的这些有很多地方感觉很亢余,不过以后再改吧,没啥子时间最近^_^

论坛徽章:
0
7 [报告]
发表于 2005-03-18 21:14 |只看该作者

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

已经很不错了,帮顶!!!

论坛徽章:
0
8 [报告]
发表于 2005-03-18 22:20 |只看该作者

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

不错的东西。

论坛徽章:
0
9 [报告]
发表于 2005-03-19 09:01 |只看该作者

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

长了见识!

论坛徽章:
0
10 [报告]
发表于 2005-03-20 04:47 |只看该作者

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

I am not familiar with shell programming. I cannot run your program on my Linux box.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP