实 验 报 告
学号 | 03093245 | 姓名 | 代严 | 课堂号 | 09078051-0 | 实验日期 | 2006/4/17 | ||||||||||
实验名称 | 实验项目三:shell及shell编程 | 实验用时 | 6 | ||||||||||||||
一、实验目的和要求 (1)、了解常用shell的编程特点,掌握shell程序设计的基础知识。 (2)、认识和理解shell程序流程控制、shell程序的运行方式、shell程序的调试方法。 (3)、基本掌握编写shell程序的步骤、方法和技巧。
二、实验内容、操作过程 (1)、显示你的unix机器上所有shell变量的名称和值,并填充下表:
% echo $HOME /c/d/a/daiyanxg % echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/games % echo $TERM ansi % echo $prompt % 如图1所示:
图1 (2)、更改shell变量PS1的值为字符串“hello”。更改后输入常用命令查看显示变化。然后再将shell变量PS1重设为原始值。 在用户家目录下新建目录“yourname”,将该目录添加进个人命令访问路径。
% set argv () cwd /c/d/a/daiyanxg history 200 home /c/d/a/daiyanxg path (/usr/local/bin /bin /usr/bin /usr/games) prompt % prompt2 ? shell /bin/csh status 0 term ansi user daiyanxg % % env TERM=ansi HOME=/c/d/a/daiyanxg SHELL=/bin/csh LOGNAME=daiyanxg USER=daiyanxg REMOTEHOST=202.103.38.14 MAIL=/var/mail/daiyanxg PATH=/usr/local/bin:/bin:/usr/bin:/usr/games PWD=/c/d/a/daiyanxg MESG=mesg MANPATH=/usr/local/man:/usr/share/man EDITOR=pico TERMCAP=ansi:u6=/E[%i%d;%dR:u7=/E[6n:..u8=/E[?%[;0123456789]c:u9=/E[c:AX:Co#8:NC #3:pa#64:AB=/E[4%p1%dm:AF=/E[3%p1%dm:op=/E[39; 49m :S2=/E[ 11m :S3=/E[ 10m :mb=/E[ 5m :m d=/E[ 1m :me=/E[0; 10m :mk=/E[ 8m :mr=/E[ 7m :..sa=/E[0;10%?%p1%t;7%;%?%p2%t;4%;%?%p3%t; 7%;%?%p4%t;5%;%?%p6%t;1%;%?%p7%t;8%;%?%p9%t;11%;m:se=/E[m:so=/E[ 7m :ue=/E[m:us=/E [ 4m :ac=+/020/054/021-/030.^Y0/333/140/ 004a / 261f / 370g /361h/260j/331k/ 277l / 332m /30 0n/305o~p/304q/304r/304s_t/303u/264v/301w/302x/263y/363z/362{/343|/330}/234~/376 :ae=/E[ 10m :as=/E[ 11m :5i:AL=/E[%dL:DC=/E[%dP:DL=/E[%dM:DO=/E[%dB:IC=/E[%d@:LE=/E[ %dD:RI=/E[%dC:SF=/E[%dS:SR=/E[%dT:UP=/E[%dA:cb=/E[1K:ch=/E[%i%dG:ct=/E[ 2g :cv=/E[ %i%dd:ec=/E[%dX:ei=:im=:kB=/E[Z:kI=/E[L:kb=^H:kd=/E[B:kl=/E[D:kr=/E[C:ku=/E[A:nw =/r/E[S:pf=/E[4i:po=/E[5i:..rp=%p1%c/E[%p2%{1}%-%db:s0=/E(B:s1=/E)B:s2=/E*B:s3=/ E+B:ta=/E[I:am:bs:mi:ms:co#80:it#8:li#24:al=/E[L:bl=^G:bt=/E[Z:cd=/E[J:ce=/E[K:c l=/E[H/E[J:cm=/E[%i%d;%dH:cr=^M:ct=/E[ 2g :dc=/E[P:dl=/E[M:do=/E[B:ho=/E[H:kb=^H:k d=/E[B:kh=/E[H:kl=/E[D:kr=/E[C:ku=/E[A: % % set TT='date' % echo $TT date % set TT=`date` % echo $TT Wed Apr 19 01:33:46 EDT 2006 % echo "The time is `date` " The time is Wed Apr 19 01:39:02 EDT 2006 % echo $prompt % % set prompt = HELLO HELLO HELLOset prompt = '%' %
%/bin/sh $ $ echo $PS1 $ $ echo $$ 4527 $ echo $# 3 $ echo $? 0 $ echo $!
$ $ /bin/csh % %mkdir youyname %ls -l total 28 drwxr-xr-x 2 daiyanxg people 512 Apr 11 07:39 file1 drwxr-xr-x 4 daiyanxg people 512 Apr 10 12:53 file2 drwxr-xr-x 2 daiyanxg people 512 Apr 25 07:49 myshell -rwxrwxrw- 2 daiyanxg people 16 Apr 11 07:47 test2 -rwxrwxrw- 2 daiyanxg people 16 Apr 11 07:47 test3 drwxr-xr-x 2 daiyanxg people 512 Apr 25 07:08 youname drwxr-xr-x 2 daiyanxg people 512 Apr 25 10:11 youyname %echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/games %echo $path /usr/local/bin /bin /usr/bin /usr/games %set path = ($path /daiyanxg/yourname/bin) %echo $path /usr/local/bin /bin /usr/bin /usr/games /daiyanxg/yourname/bin %
(3)、创建一个shell脚本文件包含date、who和echo $PATH命令: $ vi sh3.sh
#! /bin/sh date who echo $PATH sh3.sh: new file: 6 lines, 33 characters ~ ~ ~ ~ . $ chmod 755 sh3.sh $ sh sh3.sh Wed May 10 00:14:22 EDT 2006 azv ttyp0 May 9 23:15 (157.95.31.68) daiyanxg ttypl May 10 00:02 (202.103.38.14) //实验用户 doctor4u ttypm May 9 23:59 (216.139.109.76) d2004 ttypr May 10 00:05 (203.52.130.8) …… //删除一些信息 pedro112 ftp18819 May 6 08:42 (dsl5400BDA9.pool.t-online.hu) /usr/local/bin:/bin:/usr/bin:/usr/games $ 上述过程图示:
(3)、设计一个菜单驱动程序。如下: Use one of the following options: D: To display today’s date and present time L: To see the listing of files in your present working directory W: To see who is logged in Q: To quit this program Enter your option and hit <Enter>: 菜单程序将根据用户输入的选择项给出相应信息。要求对用户的输入忽略大小写,对于无效选项的输入给出相应提示。 程序代码如下:
[Kenu.sh: new file: line 1 #! /bin/sh # mymenu.sh echo "use one of the following options:" echo "D:To display today's date and present time" echo "L:To see the listing of files in youy present working directory" echo "W:To see who is logged in" echo "Q:To quit the program" read option case "$option" in D) date;; L) ls;; W) who;; Q) exit 0;; *) echo "invalid option:try running the program again" exit 1;; esac exit 0 ~
执行结果如下: $ chmod 755 mumenu.sh $ sh mumenu.sh use one of the following options: D:To display today's date and present time L:To see the listing of files in youy present working directory W:To see who is logged in Q:To quit the program $ sh mumenu.sh use one of the following options: D:To display today's date and present time L:To see the listing of files in youy present working directory W:To see who is logged in Q:To quit the program W kingjon tty01 Jun 7 13:28 pikku ttyp0 Jun 7 12:14 (dsl-lprgw5-fe5ddc00-112.dhcp.ine) barlone ttyp1 Jun 6 22:17 (ns.prosoft.ural.ru) djarek ttyp2 Jun 7 12:58 (eaq122.internetdsl.tpnet.pl) donraull ftp26895 Jun 4 14:56 (84-252-8-131.cable.evrocom.net) pchihao ftp12477 May 19 05:49 (222.253.76.86) mbb ftp28059 May 20 15:46 (p5491B806.dip0.t-ipconnect.de) $ sh mumenu.sh use one of the following options: D:To display today's date and present time L:To see the listing of files in youy present working directory W:To see who is logged in Q:To quit the program L eg5_3.sh eg5_7.sh menu.sh mumenu.sh sh1 sh2.sh sh3.sh $ sh mumenu.sh use one of the following options: D:To display today's date and present time L:To see the listing of files in youy present working directory W:To see who is logged in Q:To quit the program Q $ sh mumenu.sh use one of the following options: D:To display today's date and present time L:To see the listing of files in youy present working directory W:To see who is logged in Q:To quit the program a invalid option:try running the program again $ 以上过程如下图所示:
(4)、编写一段b shell程序完成:根据从键盘输入的学生成绩,显示相应的成绩等级,其中60分以下为“Failed!”,60-70分为“Passed!”,70-80分为“Medium!”,80-90分为“Good!”,90-100为“Excellent!”。 程序代码如下: #! /bin/sh
echo "**" echo "Enter Q to quit this program." echo "**"
while [ true ] do read score if [$score = q -o $score = Q ] then exit 0 elif [ $score -lt 60 ] then echo "Failed!" elif [$score -ge 60 -a $score -lt 70 ] then echo "Passed!" elif [ $score -ge 70 -a $score -lt 80 ] then echo "Medium!" elif [ $score -ge 80 -a $score -lt 90 ] then echo "Good!" elif [ $score -ge 90 -a $score -le 100 ] then echo "Excellent!" else echo "Error input!" fi done 如下图所示:
执行结果如下: $ chmod 755 score.bsh $ ls –l score.bsh -rwxr-xr-x 1 daiyanxg people 475 Jun 8 06:50 score.bsh $ sh score.bsh ** Enter Q to quit this program. ** 50 score.bsh[32]: [50: not found Failed! 60 score.bsh[32]: [60: not found score.bsh[32]: [60: not found Error input! 75 score.bsh[32]: [75: not found score.bsh[32]: [75: not found Medium! 85 score.bsh[32]: [85: not found score.bsh[32]: [85: not found Good! 91 score.bsh[32]: [91: not found score.bsh[32]: [91: not found Excellent! 100 score.bsh[32]: [100: not found score.bsh[32]: [100: not found Excellent! 如下图所示:
(5)、编写一段b shell程序完成:根据用户输入的主目录中的给定子目录,显示其下所有的.txt和.sh文件名的列表。 程序代码如下: [Kw.bsh: new file: line 1 #! /bin/sh if [ $# -eq 0 ] then echo "Usage:$0 directory" else for myDir in $* do if test -d $myDir then echo "In $myDir" cd $myDir echo 'ls *.txt *.sh color' else echo "Error:Direct $myDir cant't found!" fi done fi exit 0; show.bsh: new file: 20 lines, 236 characters ~ ~ ~ ~ $ cat show.bsh
#! /bin/sh if [ $# -eq 0 ] then echo "Usage:$0 directory" else for myDir in $* do if test -d $myDir then echo "In $myDir" cd $myDir echo 'ls *.txt *.sh color' else echo "Error:Direct $myDir cant't found!" fi done fi exit 0;
执行结果: $ chmod 755 show.bsh $ sh show.bsh Usage:show.bsh directory $
三、回答问题 略 四、实验结论 本实验完成了实验全部要求,练习了使用vi编辑器书写和运行shell程序的基本方法,练习了c shell和b shell的基本命令和控制流。
|