LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1038|回复: 1

Bash Shell版通讯录系统脚本

[复制链接]
发表于 2005-7-29 13:45:10 | 显示全部楼层 |阅读模式
以前在网上发布过该脚本,不过当时的程序有不少的Bug,经过修正后,基本解决了发现的问题,并加上了一些注释,具体如下:
main:(主函数脚本文件)
#!/bin/sh

# 激活下面文件中定义的函数
source unit
source addperson
source showperson
source delperson
source editperson

function menu()
{
# 主菜单函数

trap ':' 0 1 2 15;   #忽略强制中断信号

echo -e "\33[34m"
cat << EOF
        What do you want to do? Please input you choice:

                [1] Add a person
                [2] Find out a person's data
                [3] Delete a person
                [4] Edit a person's origin data
                [5] Help Document
                [6] Quit u_u
       
EOF
echo -ne "lease select [1, 2, 3, 4 ,5 or 6]: \33[0m"

#菜单选项处理
while read choice
        do
                case $choice in
                        1 ) add;;
                        2 ) show ;;
                        3 ) delete ;;
                        4 ) edit ;;
                        5 ) cat helpdoc; menu ;;
                        6 ) echo ByeBye!; exit 0 ;;
                        * ) echo -ne "\33[31mError! Please input [ 1, 2, 3, 4 or 5 ]:  \33[0m";;
                esac
done
}

checkfile #判断addressbook文件是否存在
echo -e "\33[32m\t\tWellcome to My addressbook!\33[0m"
menu  #调用主菜单函数,程序入口

addperson:(添加用户脚本文件)
function checkperson ()
{
# 用来判断名称的格式是否合法

        name=`echo "$name" | tr ':' ' '`                #去除“:”,避免数据存储错误

# 下面判断输入名称是否为空,如果为空变量则视为出错
        if [ -z "$name" ]; then
                echo -e "\33[31mError! The Name can\047t be empty! Please try again!\33[0m"
                addperson
        fi

#  下面判断输入名称是否已经存在,如果存在则有三种选择:覆盖or删除or跳过
        showname
        if [ $? -eq 0 ]; then
                echo  -ne  "\33[31mThe name you input has already exist! Do you want to delete the origin name? \33[0m\n\t\t\t[ y or n or q ( quit to main menu )]: "
                response
        #如果选择"Y",则将原名字及其资料删除
                if [ "$answer" == "y" ]; then
                        oldname=$name
                        unset name
                        update
                        if [ $? -eq 1 ]; then                #判断名字及其资料是否删除成功
                                echo The origin name is deleted now!
                                name=$oldname
                                unset oldname
                        fi
        #如果选择“N”,则提示重新输入名字
                elif [ "$answer" = "n" ]; then
                        echo -n "Input a New Name Again: "
                        read name
                        checkperson
                        check
                        identify
                fi
        fi
}

function checkmail ()
{
# 判断E-mail格式是否合法

        if [ -z $mail ]; then mail=`echo -ne "$name@$name.com"`; fi                #如果E-mail为空,则E-mail默认为name@name.com

#下面通过判断mail_check是否为空来判断E-mail的合法性
        mail_check=`echo $mail | sed -n '/\@*\./p'`
        if [ -z $mail_check ]
                then
                        echo -ne "\33[31mThe E-mail you input is incorrect!\nPlease input again: \33[0m"
                        read  mail
                        checkmail
        fi
}

function checkphone ()
{
# 判断phone number的合法性

        # 下面中判断如果phone number为空,则将其赋值为0
        if [ -z "$phone" ]; then phone=0; fi
        if [ -z "$mobile" ]; then mobile=0; fi

#下面通过判断phone_check、mobile_check是否为空来判断phone number的合法性
        phone_check=`echo $phone | tr '[0-9])(-;' ' ' `
        mobile_check=`echo $mobile | tr '[0-9])(;-' ' ' `
        if [ ! -z $phone_check ]
                then
                        echo -ne "\33[31mThe Phone Number you input is incorrect!\nPlease input again: \33[0m"
                        read phone
                        checkphone
        elif [ ! -z $mobile_check ]
                then
                        echo -ne "\33[31mThe Mobile Phone Number you input is incorrect!\nPlease input again: \33[0m"
                        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" "Save and exit to the main menu"
                do
                case $pick in
                        Name )
                                echo -n "lease input the name again: "
                                read name
                                checkperson
                                redo
                        ;;
                        E-mail )       
                                echo -n "lease input the E-mail again: "
                                read mail
                                checkmail
                                redo
                        ;;
                        Address )
                                echo -n "lease input the address again: "
                                read address
                                redo
                        ;;
                        "hone Number" )
                                echo -n "lease input the phone number again: "
                                read phone
                                checkphone
                                redo
                        ;;
                        "Moblie Phone Number" )
                                echo -n "lease input the mobile phone number again: "
                                read mobile
                                checkphone
                                redo
                        ;;
                        "Other Messages" )
                                echo -n "lease input the other messages again: "
                                read message
                                redo
                        ;;
                        "Save and exit to the main menu" )
                                update
                                menu
                        ;;
                        "Exit to the identify menu" )
                                identify
                        ;;
                        * )        echo -ne "\33[31mThis is not a valid choice. Try again: \33[0m"
                                pick=
                        ;;
                esac
        done
}

function identify ()
{
# 确认信息的正确性
        echo -e "\n\33[33mThe data you input is on the next:\33[32m"
        cat << DATA

                Name: $name
                E-mail: $mail
                Address: $address
                Phone Number: $phone
                Mobile Phone Number: $mobile
                Other Message: $message

DATA
        echo -ne "\33[31mIs it right or not?[y or n]: \33[0m"
        read answer
        if [ "$answer" = "y" ]; then
                update
                if [ $? -eq 0 ]; then
                        echo The Data is updated successfully!
                fi
                echo -e "\33[32mWhat you want to do next ? Select in the main menu!\33[0m"
                menu
        elif [ "$answer" = "n" ]; then
                echo -e "\33[35mWhich one you want to change? Pick out it : \33[0m"
                redo
        else
                echo -e "\33[35mError! Please input [y or n]!\33[0m"
                identify

        fi
}
       
function check ()
{
        checkperson
        checkmail
        checkphone
        address=`echo "$address" | tr ':' ';'`
}

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
}

showperson:(查询用户脚本文件)
function confirm ()
{
        response
        if [ "$answer" = "y" ]; then
                show
        else menu
        fi
}

function showfirst()
{#打印用户信息
         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 showname ()
{
        showfirst
        showname=`echo "$showname" | tr '^' ':'`
        if [ -z "$showname" ]; then
                return 1
        else  return 0
        fi
}

function showall ()
{#显示所有用户名,并排序输出
        awk -F: '{ print $1 | "sort"; }' addressbook | awk '{ printf "%d. %-10s\t\t",NR,$0; if ( NR%5==0 ) printf "\n";}END{ printf "\n";}'
}


function show ()
{#查询用户信息主函数
        echo -ne "\33[33mWhose data  you want to know ? \33[34mType his or her name directly \n\33[0m[ Type "all" to show all name, and No input will skip to main menu!]:  "
        read name
        if [ "$name" == "all" ]; then
                showall
                show
        elif [ -z "$name" ]; then
                menu
        else
                showname
                if [ $? -eq 1 ]; then
                        echo -e "\33[31mThe person you want to know does not exist! \33[34mCheck again!\33[0m"
                        show
                else
                        echo -e "$showname"
                        echo -ne "\33[33mDo you want to check another person's data? \33[0m["y" to continue and "q" will return to the main menu]: "
                        confirm
                fi
        fi
}

delperson:(删除用户脚本文件文件)
function deldata ()
{ #删除用户名及其对应的信息
        showname
        if [ $? -eq 1 ]; 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 ]: "
                response
                if [ "$answer" == "y" ]; then
                        oldname=$name
                        unset name
                        update
                        if [ $? -eq 1 ]; then
                                echo The origin name is deleted now!
                                echo Do you want to delete another person\'s data? [ "y" to continue and "q" will return to the main menu]
                                response
                                if [ "$answer" == "y" ]; then
                                        delete
                                else menu
                                fi
                        fi

                else
                        echo -e "\33[31mThe person's data you want to delete may not exist!\n\33[0m\tSkip to the main menu ->"
                        menu
                fi
        fi
}
               
function delete ()
{//删除用户的初始化选项
        echo -ne "\33[31mType the name you want to delete!\n\33[0m[Type all to show all names and No input will skip to main menu! ]:  "
        read name
        if [ -z "$name" ]; then
                menu
        elif [ "$name" == "all" ]; then
                showall
                delete
        else
                deldata
        fi
}

editperson:(编辑用户脚本文件)
function group ()
{ #获取要编辑用户的信息
        local i=2
        for datus in "mail" "address" "phone" "mobile" "message"
        do
                eval "$datus='`echo "$showname" | awk -F: -v num=$i '{ if ( NR == num ) print $2; }'`'"
                let  i++
        done
        message=`echo "$message" | tr '^' ':'`
}

function edit ()
{#编辑主函数
        echo -ne "\33[34mWhose data you want to edit?\33[0m\n [ Type "all" to show all names, and No input will skip to main menu! ]:  "
        read
        if [ "$REPLY" == "all" ]; then
                showall
                edit
        elif [ -z "$REPLY" ]; then
                menu
        else
                name=$REPLY
                showname > /dev/null
                if [ $? -eq 1 ]; then
                        echo -e "\33[35mThe person you want to edit does not exist! Please try again! \33[0m"
                        edit
                else
                        echo -e "The person's data is :\n\33[32m$showname\33[0m\nNow you can change it as follow [make a chioce]:"
                        showfirst
                        group
                        oldname=$name
                        unset name
                        update
                        name=$oldname
                        unset oldname
                        redo
                fi
        fi
}

unit:(公用函数脚本文件)
function checkfile ()
{#判断addressbook文件是否存在
        if [ ! -f  "addressbook"  ]; then
                echo > addressbook
        fi
}

function response ()
{#判断输入选项
        unset answer
        while read
        do
                case $REPLY in
                        y|Y|[Yy][Ee][Ss] ) answer="y";break ;;
                            n|N|[Nn][Oo] ) answer="n";break;;
                                        q|Q )  menu;;
                                    * )  echo  "Error! Please input y or n or q (quit to main menu )!" ;;
                esac
        done
}

function update ()
{#更新addreebook信息
        awk -F: -v new=$oldname '{ if ( $1 != new ) print $0; }' addressbook  > /tmp/tmp$$
        mv -f /tmp/tmp$$ addressbook 2> /dev/null
        if [ ! -z "$name" ]; then
                message=`echo $message | tr ':' '^'`
                echo $namemailaddressphonemobilemessage >> addressbook
                return 0
        else  return 1
        fi
}

helpdoc:(帮助文档)
===========================================================================================================================
                            Thank you for using the address book system !
===========================================================================================================================

    1.First, if you choose 1 from the menu, then you can add a person into the addressbook. But there are some definition you should follow:
        (1) The name cannot empty and repeated with the already exist name
        (2) The E-mail should follow the currect format
        (3) The phone number can be added area-code in it, such as 1234-12345678, (1234)12345678......
   2. while using the edit option, do careful not to kill the process forcely, this may cause you lose the person's data!

===========================================================================================================================
                                                END
===========================================================================================================================
发表于 2005-7-30 14:25:26 | 显示全部楼层
不错~~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表