文档详情

Linux操作系统原理与应用第9章解析.ppt

发布:2016-10-21约字共142页下载文档
文本预览下载声明
     case $choice in       e*) vi $i;;       v*) cat $i;;       r*) rm $i;;       n*) break;;       q*) exit 0;;        *) echo Illegal Option;;      esac     done    done   $ procfile file1 file2 file3    file1: Edit, View, Remove, Next, Quit? [e|v|r|n|q]: n    file2: Edit, View, Remove, Next, Quit? [e|v|r|n|q]: v    …(显示file2的内容)    file2: Edit, View, Remove, Next, Quit? [e|v|r|n|q]: r    file2: Edit, View, Remove, Next, Quit? [e|v|r|n|q]: n    file3: Edit, View, Remove, Next, Quit? [e|v|r|n|q]: q   $   第1行以#!打头的注释行指示Shell应使用bash来执行此脚本。脚本执行时需要带若干个文件名作为运行参数。for循环用于依次处理参数中的各个文件。对每个文件的处理过程是一个while循环,它先列出可执行的操作的菜单,然后读取用户的输入,再通过case结构根据输入对文件进行指定的操作。当输入n时结束对这个文件的处理,进入下一个文件的处理过程。输入q时退出程序。   例9.57 一个求数字累加和的程序:   $ cat addall    #!/bin/bash    if [ $# = 0 ] then     echo “Usage: $0 number-list”     exit 1    fi    sum=0 # sum of numbers    count=$# # count of numbers    while [ $# != 0 ]    do    sum=`expr $sum + $1`    shift    done    # display final sum    echo “The sum of the given $count numbers is $sum.”    exit 0   $ addall    Usage: addall number-list   $ addall 1 4 9 16 25 36 49    The sum of the given 7 numbers is 140.   $   addall脚本以一系列整数为参数,对它们求和。脚本首先检查参数个数,如果没有参数则提示命令的用法并退出。?脚本通过while循环将参数表中的参数一个个累加到sum变量中,然后显示累加结果。   例9.58 一个脚本命令groups,用于求用户所在的用户组的名称:   $ cat /usr/bin/groups    #!/bin/sh    usage=“Usage:     $0 --help display this help and exit     $0 --version output version information and exit     $0 [user]… print the groups a user is in”    fail=0    case $# in     1) case “$1” in     --help ) echo “$usage” || fail=1; exit $fail;;     --version ) echo “groups (GNU coreutils) 4.5.3” || fail=1; exit $fail;;      * ) ;;     esac;;     * ) ;;   esac   if [ $# -eq 0 ]; then    id -Gn  #求当前用户的组名    fail=$?   else    for name in “$@”; do     groups=`id -Gn -- $name`  #求$name用户的组名     status=$?     if test $status = 0; then     echo $name : $groups    else     fail=$status    fi    done
显示全部
相似文档