专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

shell函数应用实例介绍

百度收藏 QQ搜藏

代码:
$cat dbfunctions
addon () {                  # 定义函数addon,他的功能是把新的信息加入datafile
    while true
    do
        echo "Adding information "
        echo "Type the full name of employee "
        read name
        echo "Type address for employee "
        read address
        echo "Type start date for employee (4/10/88 ) :"
        read startdate
        echo $name:$address:$startdate
        echo -n "Is this correct? "
        read ans
        case "$ans"  in
        [Yy]*)
            echo "Adding info..."
            echo $name:$address:$startdate>>datafile
            sort -u datafile -o datafile
            echo -n "Do you want to go back to the main menu? "
            read ans
            if [ $ans = Y -o $ans = y ]
            then
                return        # return命令把控制送回函数被调用时所在的调用程序          
      else
                continue        # 把控制返回到while循环顶部
            fi
                ;;
        *)
            echo "Do you want to try again? "
            read answer
            case "$answer" in
                [Yy]*)
                    continue;;
                *)
                    exit;;
            esac
                ;;
        esac
    done
}       # 结束函数定义

$cat mainprog
#!/bin/sh
script name: mainprog
# This is the main script that will call the function, addon

#datafile=$HOME/bourne/datafile
datafile=./datafile

.  dbfunctions  # dot命令把文件dbfunctions装入内存,

if [ ! -f $datafile ]
then
    echo "`basename $datafile` does not exist" 1>&2
    exit 1
fi
echo "Select one: "
cat <<EOF
    [1] Add info
    [2] Delete info
    [3] Exit
EOF
read choice
case $choice in
    1)  addon   # 调用函数
        ;;
    2)  delete  # 调用函数
        ;;
    3)  update
        ;;
    4)  echo Bye
        exit 0
        ;;
    *)  echo Bad choice
        exit 2
        ;;
esac
echo Returned from function call
echo The name is $name
# Variable set in the function are known in this shell.

附:文件datafile
cat database
Ann Stephens            111 Main St, Boston, MA         4/10/88
TB Savage               222 B Ave, New York, NY         5/11/99

上一篇:逐行校正文件内容正确与否,起编辑的作用 下一篇:锁定控制台的一个脚本

power by soyo123 2007-2008