linux上shell程序设计实验报告

上传人:第*** 文档编号:61592206 上传时间:2018-12-05 格式:DOC 页数:18 大小:477KB
返回 下载 相关 举报
linux上shell程序设计实验报告_第1页
第1页 / 共18页
linux上shell程序设计实验报告_第2页
第2页 / 共18页
linux上shell程序设计实验报告_第3页
第3页 / 共18页
linux上shell程序设计实验报告_第4页
第4页 / 共18页
linux上shell程序设计实验报告_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《linux上shell程序设计实验报告》由会员分享,可在线阅读,更多相关《linux上shell程序设计实验报告(18页珍藏版)》请在金锄头文库上搜索。

1、深 圳 大 学 实 验 报 告 课程名称: Linux操作系统 实验项目名称: Linux上shell程序设计 学院: 计算机与软件学院 专业: 软件工程 指导教师: 冯禹洪 报告人: 文成 学号: 2011150259 班级: 2 实验时间: 2013-10-08 实验报告提交时间: 2013-10-30 教务处制一、 实验目标:1. 熟悉vi编辑器的使用2. 掌握简单的Shell编程2.1. 掌握Shell变量的使用2.2. 掌握Shell表达式的使用2.3. 掌握Shell流程控制语句的使用3. 熟悉Shell程序的执行和跟踪二、实验环境与工件湖边Linux实验室 Fedora 13三、

2、实验内容与步骤1. 下面哪个命令会实现将变量VAR1和VAR2相加,并且值存入变量VAR3的功能?( D )(5分) $VAR3 = $VAR1 + $VAR2 $VAR3 = $VAR1 + $VAR2 $VAR3 = ( VAR1 + VAR2 )( VAR3 = VAR1 + VAR2 )2. 以下程序的输出是?( D )(5分)#!/usr/bin/shA=1while $A -lt 10 do B=1 while $B -lt 10 do break 2 echo Inner loop done echo Outer loopdoneA. “Inner loop” 将被打印10次B.

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

4、:bbbbecho -n Do you want to continue: Y or Nread ANSWERif $ANSWER=N -o $ANSWER=n then exitfi3.3.使用if-then-else语句创建一个根据输入的分数判断是否及格的shell程序。#!/bin/bash#filename:akecho -n please input a score:read SCOREecho You input Score is $SCOREif $SCORE -ge 60 ;then echo -n Congratulation!You Pass the examination

5、.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 menuecho _echo 1 Restoreecho 2 Backupecho 3 Unloadecho #Read and excute the users selectionecho -n Enter Choice:read CHOICEcase $CHOICE in1) echo

6、Restore;2) echo Backup;3) echo Unload;*) echo Sorry $CHOICE is not a valid choiceexit 1esac3.5.使用for语句创建简单的shell程序。#!/bin/bash#filename:mmfor ab in 1 2 3 4do echo $abdone3.6.使用for语句创建求命令行上所有整数之和的shell程序。#!/bin/bash#filename:qqqsum=0for INT in $*do sum=expr $sum + $INTdone echo $sum3.7.使用while语句创建一个计

7、算1-5的平方的shell程序。#!/bin/bash#filename:zxint=1while $int -le 5 do sq=expr $int * $intecho $sqint=expr $int + 1doneecho Job completed3.8.使用while语句创建一个根据输入的数值求累加和(1+2+3+4+n)的shell程序。#!/bin/bash#filename:sumecho -n Please Input Number:read NUMnumber=0sum=0while $number -le $NUM doecho numberecho $numbern

8、umber= expr $number + 1 echo sumecho $sumsum= expr $sum + $number done echo3.9.使用until语句创建一个计算1-5的平方的shell程序。#!/bin/bash#filename:xxint=1until $int -gt 5 dosq=expr $int * $intecho $sqint=expr $int + 1done echo Job completed3.10.使用until语句创建一个输入exit退出的shell程序。#!/bin/bash#filename:hkecho This example i

9、s for test until.do echo If you input exit then quit the system echo -n please input:read EXITuntil $EXIT = exit do read EXITdoneecho OK!4. 写一个Shell脚本,接收两个文件名作为参数。该脚本将检查两个文件内容是否一样,如果一样就删除第二个文件。请附程序和运行结果截图(5分)5. 写一个Shell脚本来检查当前目录中所有文件的可执行权限,如果存在一个文件没有可执行权限,则为其添加可执行权限。请附程序和运行结果截图(5分)6. 编写一个shell脚本,输入1

10、 10之间的一个数,并判断它是否小于5。请附程序和运行结果截图,请注意程序的易读性。(10分)7. 编写一个shell脚本,随机输入5个数,打印出了值为3的倍数的所有数。(10分)四、实验结果1.D2.D3.3.13.23.33.43.53.63.73.83.93.104. 写一个Shell脚本,接收两个文件名作为参数。该脚本将检查两个文件内容是否一样,如果一样就删除第二个文件。请附程序和运行结果截图#!/bin/bashif -z $1 ; then echo Input file 1 and file 2 exitfiif -z $2 ; thenecho Input file 2 exi

11、tfiif ! -f $1 ; then echo $1 doesnt exist. exitfiif ! -f $2 ; thenecho $2 doesnt exist. exitfiif cmp $1 $2thenecho Both files content are the samerm -f $2if $? -eq 0 ;thenecho -e File $2 has been delete successfully.nelseecho -e Error deleting file $2.Please check whether it is a regular file or not

12、.nfielseecho -e File $1 and $2 are different.nfi5. 写一个Shell脚本来检查当前目录中所有文件的可执行权限,如果存在一个文件没有可执行权限,则为其添加可执行权限。请附程序和运行结果截图解:#!/bin/bashclears=0x=0for file in *.*dos=expr $s+1if ! -s $file ;thenif chomod u+x $filethenecho Successfully add executable permission to $filex=expr $x+1elseecho fail to add executable permission to $filefifidoneecho -e n Total number of file scanned:$secho -e Executable permission added to $x file.n截图:6. 编写一个shell脚本,输入

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号