Linux上Shell程序设计_第1页
Linux上Shell程序设计_第2页
Linux上Shell程序设计_第3页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

1、深圳 大学实 验报告课程名称: Linux 操作系统实验项目名称:Linux 上 shell程序设计学院专业指导教师:冯禹洪报告人:学号班级:实验时间:实验报告提交时间:教务处制一、实验目标:1. 熟悉 vi 编辑器的使用2. 掌握简单的 Shell 编程2.1. 掌握 Shell 变量的使用2.2. 掌握 Shell 表达式的使用2.3. 掌握 Shell 流程控制语句的使用3. 熟悉 Shell 程序的执行和跟踪二、实验环境与工件湖边 Linux 实验室Fedora 13三、实验内容与步骤 :准备工作 : 如果当前系统已经有学生名字加学号命名的账号,则 直接以该账号登录,然后转 2,一次完

2、成后面的实验内容。i. 创建一个以学生名字加学号命名的账号,不如,学生名字 为张山,学号为 123,则操作如下:#useradd zhangshan123#passwd zhangshan123然后以 zhangshan123 名字登录ii. 在主目录下a) 创建文件夹: ShellProgram$mkdir ShellProgramb) 进入 ShellProgram 子目录$cd ShellProgramiii. 请根据自己对 Linux 的熟悉程度选做题 (一)或题 (二) 题 (一 )1. 下面哪个命令会实现将变量 VAR1 和 VAR2 相加,并且值存入变量 VAR3 的功能? ()

3、(5 分)A. $VAR3 = $VAR1 + $VAR2 B. $VAR3 = $VAR1 + $VAR2 C. $VAR3 = ( VAR1 + VAR2 )D. ( VAR3 = VAR1 + VAR2 )2. 以下程序的输出是? ()(5 分)#!/usr/bi n/shA=1while $A -It 10 doB=1while $B -lt 10 dobreak 2echo "I nner loop"doneecho "Outer loop"doneA."1 nnelroop”将被打印10次B."Outetoop ”将被打印

4、10 次.C."Outetoop ”将被打印10 次.D.什么都没有被打印.3. 请在vi中逐一编辑,修正(如果有误)并执行以下10个shell脚本程序,然后结合所学知识和程序的输出理解各程序中各语句的含义,对各小题附上结果截图。(每小题5分)3.1. 编写一个简单的回显用户名的shell程序。#!/bi n/bash #file name:dateecho "Mr.$USER,Today is:"echo 'date'echo Whish you a lucky day!3.2. 使用if-then语句创建简单的shell程序。#!/bi n/b

5、ash#filename:bbbbecho -n "Do you want to continue: Y or N" read ANSWERif $ANSWER=N -o $ANSWER=n then exit fishell 程序。3.3. 使用 if-then-else 语句创建一个根据输入的分数判断是否及格的 #!/bin/bash#filename:ak echo -n "please input a score:" read SCOREecho "You input Score is $SCORE"if $SCORE -ge

6、 60 ;thenecho -n "Congratulation!You Pass the examination." elseecho -n "Sorry!You Fail the examination!"fiecho -n "press any key to continue!"read $GOOUT3.4. 使用 case 语句创建一个菜单选择的 shell 程序。 #!/bin/bash#filename:za#Display a menu echo _ echo "1 Restore" echo &qu

7、ot;2 Backup" echo "3 Unload" echo#Read and excute the user's selectionecho -n "Enter Choice:" read CHOICE case "$CHOICE" in1) echo "Restore"2) echo "Backup"3) echo "Unload"*) echo "Sorry $CHOICE is not a valid choice exit 1 es

8、ac3.5. 使用 for 语句创建简单的 shell 程序。#!/bin/bash#filename:mmfor ab in 1 2 3 4doecho $abdone3.6. 使用 for 语句创建求命令行上所有整数之和的shell 程序。#!/bin/bash #filename:qqq sum=0 for INT in $* do sum='expr $sum + $INT' done echo $sum3.7. 使用 while 语句创建一个计算 1-5 的平方的 shell 程序。#!/bin/bash#filename:zxint=1while $int -le

9、5 dosq='expr $int * $int'echo $sqint='expr $int + 1'doneecho "Job completed"38使用while语句创建一个根据输入的数值求累加和(1+2+3+4+n)的shell程序。#!/bin/bash#filename:sumecho -n "Please Input Number:"read NUMnumber=0sum=0while $number -le $NUM doecho numberecho "$number"number=

10、' expr $number + 1 'echo sumecho "$sum"sum=' expr $sum + $number 'done echo 3.9.使用 until 语句创建一个计算 1-5 的平方的 shell 程序。#!/bin/bash#filename:xxint=1until $int -gt 5 dosq='expr $int * $int'echo $sq int='expr $int + 1'doneecho "Job completed"3.10. 使用 unt

11、il 语句创建一个输入 exit 退出的 shell 程序。#!/bin/bash#filename:hkecho "This example is for test untildo "echo "If you input exit then quit the system "echo -n "please input:"read EXITuntil $EXIT = "exit" doread EXITdoneecho "OK!"4. 写一个 Shell 脚本,接收两个文件名作为参数。该脚本将检

12、查两个文件内容是否一样,如 果一样就删除第二个文件。请附程序和运行结果截图(5 分)参考代码:1 r!/bin/bash67 fiechoFor ea«ple:sh $e abc/txt Myz.txt"S9 if 1-e $1i *16 then11echo-e ' si doesnot existixn"12exit113 fi1415 if | 1-e $216 then17chot - 32not exis-t Xn"exit119 fl2G21 if cnpSi $222 then23echoBoth file'sCQntemt

13、 re the24r» -1f $225if 5? -eq £ I;26then27echo -e "File tz h节 beei#leleted successfully.n"2Selse25echo -e "Error deleting file K, Please check whether抽fi31 else32ech 口-e Files SIand $2 min? different An'33 flthen'USNgR;刮hffilnafiit 11a regular file or not An"测试:

14、y u hongF ed o raD VD13 y u ho ngF ed o raD VD13 Both file's content File file2 has beenfilel file2test$ cp filel file2 test$ ./compareFiles.sh are the same.deleted successfully.5. 写一个Shell脚本来检查当前目录中所有文件的可执行权限,如果存在一个文件没有可执行权限,则为其添加可执行权限。请附程序和运行结果截图(5分)参考代码:#!/bin/bashclear£=0x=&|Lfor fi

15、le in *.*dos= expr + 1'if ! -x ;t heni f chnod u+x $filethenr :ha successfully add executable permission tox=Lepr $)c + 1elsel "Fail to add executable pemission tofifitchQ -e 'n Total number cf iiles scanned; echo -e Executable pemisEion added to files.nH测试:yuhongFedoraDVD13 shell$ Isde

16、clare shreadAchar.shstrcmp.sh test2. stitest.shjetFileType.sreadline.shtestExecutablE*5hyuhongFedoraDVD13 shell$ /bin/bash testExecutable.shSuccessfully sdd Successfully add Successfully addexecutable permission executable permission executable permissiontototoreadAcharsh readline+sh testExecutable

17、shTotal number offiles scanned: 9Executable permission added to 3 files yuhongFedo raDVD13 shellS Isi栏8 shrealdAchdr* $h strcmp 8 shtest?.ShAeType.,shrealsh test1IshtestExecutable * siyuhongFedoraDVD13 shelljS |6. 编写一个shell脚本,输入1 -10之间的一个数,并判断它是否小于5。请附程序和运行结果截图,请注意程序的易读性。(10分)7. 编写一个shell脚本,随机输入5个数,

18、打印出了值为 3的倍数的所有数。(20分)题(二)1. 编写shell脚本将当前目录下大于10k的文件转移到/tmp目录下(30分)# vi 10K.sh#!/bi n/bash#ext-1#Usi ng for move curre ntly directory to /tmpfor File name in $(ls -l |awk '$5 > 10240 print $9')domv $File name /tmpdonels -la /tmpecho "Do ne!"2. 设计一个Shell程序,在/data/目录下建立50个目录,即user1user50,并设置每个目 录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所 有者所在组的权限为:读、执行。 (30分)#!/bin/bash#7.sh#file executable: chmod 755 7.shPATH=/b in:/sbin:/usr/b in:/us

温馨提示

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

评论

0/150

提交评论