- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Linux操作系统原理与应用第9章概要1
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
原创力文档


文档评论(0)