Shell编程实验报告_第1页
Shell编程实验报告_第2页
Shell编程实验报告_第3页
Shell编程实验报告_第4页
Shell编程实验报告_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、南京信息工程大学实验(实习)报告【实验目的】1、了解和熟悉创建并使用脚本的步骤。2、熟悉bash的控制结构。3、学会简单的shell编程。【实验内容】1、创建一个简单的列目录和日期的shell脚本并运行之。步骤:输入下列命令,创建一个新文件:cat n ew_script输入下列行:echo Your files are ”lsecho today is ”date按回车键将光标移到一个新行,按 Ctrl+D键保存并退出。检查文件内容,确保它是正确的:cat n ew_script运行月脚本,输入它的文件名:n ew_script该脚本不运行。输入下列命令,显示文件的权限:ls - l new

2、 _script权限表明该文件不是可执行。要通过简单调用文件名来运行脚本,必须有权限。输入下列命令,使new_script变成可执行文件。chmod +x n ew_script要查看新的权限,输入:ls - l现在拥有文件的读、写和执行权限。输入新脚本的名字以执行它:n ew_script所有输入到文件的命令都执行,并输出到屏幕上。如果接收到错误信息,比如:comma nd not found输入下列命令:./n ew_script该命令行通知shell到哪里寻找shell脚本new_script,即您的当前目录“.” 程序运行过程如下:一2、用Shell语言编制一 Shell程序,该程序在

3、用户输入年、月之后,自动打印 数出该年该月的日历。程序运行过程如下:root(?rhl00 root 1H cattimee echo MPlease input tl)e month: read nitonthecho R Pl ease input tl)c ycai: M read yearcal $fnonth $yearroot(3rh 100 root H bash times PloatJR input thp month:9PlpaHR input, thp ypar:2012九片 20120 一二三回五六234S9101112161718192324252630root ir

4、h 10 067813 A 1520 21 2227 23 29rootH |3、编程提示用户输入两个单词,并将其读入,然后比较这两个单词,如果两个 单词相同则显示“ Match”,并显示“ End of program ”,如果不同则显示“ End of program ”。程序运行过程如下:(rootHrhlOO rootH catilIecho n word 1:H read word 1echo n word 2 : read wotd 2if test * $word 1M = *$word2*thenecho Matchf iecho * End of program *rootr

5、hl00 rootH bash if 1word 1:goodword 2:goodHatchEnd of pirogratn.(rootrh100 root# bash if 1word 1 : goodword 2:badEnd ot progratn.rootyrhICO root# |4、修改上述程序,编程提示用户输入两个单词,并将其读入,然后比较这两 个单词,如果两个单词相同显示“ Match” ,不同则显示“ Not match ”,最后显 示“ End of program ”。请使用if , then, else 控制结构。程序运行过程如下:Irootrhl00 root| c

6、atif2echo n uword 1:11read word 1echo -n word 2:11read word2if test *$wordl M = HSword2M_thenecho Matchel se echo *Kot match* f i echo *End of program, rootrh100 roottf bash if2 word i:good word 2:goodMatchEnd of program*(root feirh 100 root ) | bah it 2word 1:word 2:noHot matchEnd ot programrootrhi

7、oo root0 |5、编程使用case结构创建一个简单的菜单,屏幕显示菜单:a. Current date and timeb. User curre ntly logged inc. Name of the worki ng directoryd. Contents of the worki ng directoryEn ter a,b,c or d:根据用户输入选项做相应操作。shell编程代码如下:echo -e *n COHMAIW MENUnM echo Current date and t line echo 11 bUser currently logged in echo c

8、,Name of the working directory,i echo * d+contents of the working directoryn echo -n HEnter a b t c or d:n read echocase Sanswer1 indate ir /b)who motiu I echo pcho echo pcho echo echo echo read echo-e n COMMAND HENUXn* a. Currpnt datp and ti(nchl* b,Uaer currently logged in1*cof thp working directo

9、ryd.contents of the working directory * e*Exit irtenunu-n Enter arbrcFd or e:while ( doSannwer1* 1= V caseh bl c)*Sanswer in date;who; ipwd;1 s jecho *Exit menur;echo MThere is no select ionAHflC-n Enter a ,btcrd or e: * answp$answer*echorpad done echo *Exit menu ln rootrh 100 root JU bash idchuIans

10、wer程序运行结果如下:rootrhICO root)# bash menulCOMI 1A ND ME HUa .Current date and tiiwbUser currently logged inc + Name of the working directoryd.Contcntu ot th芒 working directory eExit menunEnter a r b c,d orE :曰三 5片:19 10 : 39:1 3 (?ST20 10Enter a Fbrctd ore:broot _ ttylNayI9OS:09rootpts/oMay190d:09rootp

11、ts/1May沖oa:55EnLe匚_a,b,c,d ore:eEX i t menu Inroatrh100 root# |7、编程使用select结构生成一个菜单如下:1) apple 3)blueberry 5)orange 7)STOP2) banana 4)kiwi 6)watermel onChoose your favorite fruit from these possibilities:用户输入所选项,如1显示:You chose apple as you favorite.That is choice nu mber 1.shell编程代码及运行结果如下:8、 上机编写如下

12、代码,分析下面的程序,简要说明整体功能,并解释每条语句。 #!/bi n/shval=1while (test $val -It 6) do touch file$val datefile$val val=expr $val + 1 done9、编写一段shell程序,根据执行时候获取的当前时间显示出不同的问候信息。10、 用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到/dev目 录下11、在根目录下有四个文件 m1.txt, m2.txt, m3.txt, m4.txt,用Shell编程, 实现自动创建 m1,m2,m3,m4四个目录,并将 m1.txt,m2.txt,m3.txt,m4.txt 四个 文件分别拷贝到各自相应的目录下【实验小结】通过几周的实习,我基本上掌握了shell的编程,了解和熟悉创建并使用脚本的步骤;熟悉bash的控制结构;学会简单的

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论