欢迎来到天天文库
浏览记录
ID:32399667
大小:52.00 KB
页数:10页
时间:2019-02-04
《linux-实验指导手册-shell编程》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Linux实用操作系统实验指导手册实验二shell编程(8学时)一、实验名称shell编程。二、实验目的掌握shell程序的建立和执行方式;掌握shell中各种变量和函数的使用;熟练掌握shell程序设计中各种控制结构语句是使用;了解shell编程中特殊字符的含义。三、实验环境RedhatLinux系统企业版本4.0及以上四、实验内容(一)shell程序的建立和执行(2学时)1.依次执行下列命令,理解反馈信息的意义。$date$pwd$cd..$cd2.建立shell程序脚本$viex1datepwdcd..cd存盘
2、退出。3.检查文件是否存在。$ls4.执行shell程序ex1,显示反馈信息与单步命令操作相同。方式一:输入定向到shell脚本$bash3、hecurrentdirectory.#Otherwise,listingeachsubdirectory.iftest$#=0thenls.elseforidols-l$i4、grep'^d'donefi(二)shell变量(2学时)1.用户定义的变量。单步执行下述命令,练习变量赋值,理解反馈信息。$dir=/home/mengqc/ex1$echo$dir$echodir$today=Sunday$echo$today$Today$str=”HappyNewYear.”$echo"WishYou$str"2.rea5、d命令。(1)单步执行下述命令:$readname-----输入read命令zhangsan-----输入name的值$echo"YourNameis$name."YourNameiszhangsan-----显示输出的结果$readabc-----read命令有三个参数cuitcnedu-----输入三个字符串,中间以空格隔开$echo"Email:$a.$c.$b"Email:cuit.edu.cn-----显示输出结果(2)将上述四个单步命令编辑为shell程序,取名ex3。$viex3(输入四行命令,编后存盘6、)。(3)运行shell程序ex3。$bashex33.特殊变量(1)建立一个内容如下的shell程序ex4:echo“Programnameis$0”echo“Therearetotally$#parameterspassedtothisprogram”echo“Thelastis$?”echo“Theparametersare$*”(2)按如下执行程序ex4,观察反馈信息:$bashex4thisisatestprogram(三)控制结构(3学时)1.if语句(1)理解并建立shell程序ex5:echo“The7、currentdirectoryis`pwd`”iftest-f"$1" #如果位置参数$1对应的文件thenecho"$1isanordinaryfile."#是普通文件“-F”则显示本行elseecho"$1isnotan ordinaryfile."#否则显示本行fi执行ex5,并理解反馈信息:$bashex5ex4(2)理解并建立shell程序ex6:iftest-f"$1"thencat$1elseiftest-d"$1"then(cd$1;cat*)elseecho"$1isneitherafilenor8、adirectory."fifi执行ex6,并理解反馈信息。(3)验证书上例4.11、例4.12、例4.13。2.case语句验证书上例4.142.while语句(1)理解并建立shell程序ex7:while[$1]doif[-f$1]thenecho"display:$1"cat$1elseecho"$1isnotafilename."fishift#后续位置参数左移done执行ex7,并理解反馈信息。(2)编写求前五个偶数之和的shell程序ex8:loopcount=0result=0while[$loopc9、ount–lt5]do((loopcount=loopcount+1))((increment=loopcount*2))let“result=result+increment”doneecho"resultis$result"3.for语句(1)理解并建立shell程序ex9:fordayinMondayWednesdayFridaySund
3、hecurrentdirectory.#Otherwise,listingeachsubdirectory.iftest$#=0thenls.elseforidols-l$i
4、grep'^d'donefi(二)shell变量(2学时)1.用户定义的变量。单步执行下述命令,练习变量赋值,理解反馈信息。$dir=/home/mengqc/ex1$echo$dir$echodir$today=Sunday$echo$today$Today$str=”HappyNewYear.”$echo"WishYou$str"2.rea
5、d命令。(1)单步执行下述命令:$readname-----输入read命令zhangsan-----输入name的值$echo"YourNameis$name."YourNameiszhangsan-----显示输出的结果$readabc-----read命令有三个参数cuitcnedu-----输入三个字符串,中间以空格隔开$echo"Email:$a.$c.$b"Email:cuit.edu.cn-----显示输出结果(2)将上述四个单步命令编辑为shell程序,取名ex3。$viex3(输入四行命令,编后存盘
6、)。(3)运行shell程序ex3。$bashex33.特殊变量(1)建立一个内容如下的shell程序ex4:echo“Programnameis$0”echo“Therearetotally$#parameterspassedtothisprogram”echo“Thelastis$?”echo“Theparametersare$*”(2)按如下执行程序ex4,观察反馈信息:$bashex4thisisatestprogram(三)控制结构(3学时)1.if语句(1)理解并建立shell程序ex5:echo“The
7、currentdirectoryis`pwd`”iftest-f"$1" #如果位置参数$1对应的文件thenecho"$1isanordinaryfile."#是普通文件“-F”则显示本行elseecho"$1isnotan ordinaryfile."#否则显示本行fi执行ex5,并理解反馈信息:$bashex5ex4(2)理解并建立shell程序ex6:iftest-f"$1"thencat$1elseiftest-d"$1"then(cd$1;cat*)elseecho"$1isneitherafilenor
8、adirectory."fifi执行ex6,并理解反馈信息。(3)验证书上例4.11、例4.12、例4.13。2.case语句验证书上例4.142.while语句(1)理解并建立shell程序ex7:while[$1]doif[-f$1]thenecho"display:$1"cat$1elseecho"$1isnotafilename."fishift#后续位置参数左移done执行ex7,并理解反馈信息。(2)编写求前五个偶数之和的shell程序ex8:loopcount=0result=0while[$loopc
9、ount–lt5]do((loopcount=loopcount+1))((increment=loopcount*2))let“result=result+increment”doneecho"resultis$result"3.for语句(1)理解并建立shell程序ex9:fordayinMondayWednesdayFridaySund
此文档下载收益归作者所有