您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > linux的shell脚本实验报告
第二次实验内容一、实验名称:Linux下shell编程二、实验类型:设计三、实验目的:1.熟悉Linux的shell几种变量使用2.熟练掌握Linux的shell编程几种结构3.熟练掌握Linux下shell脚本的编写四、实验准备参考教材,课件第7章内容及笔记。要求实验内容全部写到实验报告上(B5纸)。五、实验内容1.练习使用shell四种变量,参考课件例题。用户自定义变量,环境变量,位置变量,特殊变量这四种变量类型的使用,书中有例题。2.调试课件所有shell脚本的例题。3.编写如下脚本:编写脚本if1,测试其功能。echo-nword1:readword1echo-nword2:readword2iftest$word1=$word2thenechoMatchfiechoEndofprogram.编写脚本chkargs,测试其功能iftest$#-eq0thenechoYoumustsupplyatleastoneargument.exit1fiechoProgramrunning.编写脚本if2,测试其功能iftest$#-eq0thenechoYoumustsupplyatleastoneargument.exit1fiiftest-f$1thenecho$1isaregularfileintheworkingdirectoryelseecho$1isNOTaregularfileintheworkingdirectoryfi编写脚本if3,测试其功能echo-nword1:readword1echo-nword2:readword2echo-nword3:readword3if[$word1=$word2-a$word2=$word3]thenechoMatch:words1,2,&3elif[$word1=$word2]thenechoMatch:words1&2elif[$word1=$word3]thenechoMatch:words1&3elif[$word2=$word3]thenechoMatch:words2&3elseechoNomatchfi编写smartzip脚本,测试其功能#!/bin/bashftype=`file$1`case$ftypein$1:Ziparchive*)unzip$1;;$1:gzipcompressed*)gunzip$1;;$1:bzip2compressed*)bunzip2$1;;*)echoFile$1cannotbeuncompressedwithsmartzip;;esac编写脚本dirfiles,测试其功能。foriin*doif[-d$i]thenecho$ifidone编写脚本until1,测试其功能。用while改写之。secretname=jennyname=nonameechoTrytoguessthesecretname!echountil[$name=$secretname]//while改写位while[“$name”!=“$secretname”],其他地方不变doecho-nYourguess:readnamedoneechoVerygood.编写脚本brk,测试其功能。forindexin12345678910doif[$index-le3];thenechocontinuecontinuefi#echo$index#if[$index-ge8];thenechobreakbreakfidone编写脚本command_menu,测试其功能。echo-e\nCOMMANDMENU\nechoa.Currentdateandtimeechob.Userscurrentlyloggedinechoc.Nameoftheworkingdirectoryecho-ed.Contentsoftheworkingdirectory\necho-nEntera,b,c,ord:readanswerechocase$answerina)date;;b)who;;c)pwd;;d)ls;;*)echoThereisnoselection:$answer;;esac编写脚本demo_shift,测试其功能。echoarg1=$1arg2=$2arg3=$3shiftechoarg1=$1arg2=$2arg3=$3shiftechoarg1=$1arg2=$2arg3=$3shiftechoarg1=$1arg2=$2arg3=$3shift编写shell脚本sum1,求命令行上整数和。即:$./sum151246,给出和的结果。sum=0foriin$*doletsum=sum+idoneecho“和是:$sum”编写脚本filetest,判断当前目录下所有文件类型,如果是普通文件,显示文件内容;如果是目录文件,显示目录列表;如果是大小为0的文件,删除它;否则,显示“sorry,Thefileisnotrecognized!”foriin*doif[-d$i]thenls$ielif[-f$i]thenif[-s$i]thencat$ielserm$ifielseecho–n“sorry,thefilecan’tberecognized”fidone编写shell脚本user,判断当前登录用户是否为“学号命名”的用户,是,提示:hello“学号用户”,welcome!,否,提示“youshouldloginusingyourusername!”ReadnameIf[$USER=$name]Thenecho“hello$USER”elseecho“youshouldloginusingyourusername!”fi编写shell脚本menu,使用shell编写一个菜单,分别实现列出以下内容:(1)显示目录内容、(2)切换目录、(3)创建文件、(4)编辑文件、(5)删除文件的功能。在此例中将用到循环语句、分支语句和输入输出语句。Echo“a.displaythedirectory”Echo“b.changethedirectory”Echo“c.createafile”Echo“d.deletethefile”Echo“ifyouinputnothing,youwillexit”ReaditemUntil[-z$item]DoCase$itemina)Echo“inputthedirectory”ReaddireLs$dire;;b)Echo“inputthedirectoryyouwantgointo”ReaddireCd$dire;;c)Echo“inputthefileyouwanttocreate”ReadfTouch$f;;d)Echo“inputthefileyouwanttodelete”ReadfRm$f;;EsacEcho“a.displaythedirectory”Echo“b.changethedirectory”Echo“c.createafile”Echo“d.deletethefile”Echo“ifyouinputnothing,youwillexit”Readitemdone编写脚本,实现一个简单计算器。+addition-subtractionxmultiplication/division脚本执行形式:$./cal.sh21/3Letl=$1//最简单的一种形式,而且还特别高效Echo$1;第二种方式:Re=”+-\*/”Forvarin$reDoIf[$var=“\*”]ThenNum2=${1#*\*}Num1=${1%\**}ElseNum2=${1#*$var}Num1=${1#%$var*}FiIf[$num1=$1]ThencontinueFiCase$varin“+”)letnum=num1+num2Break;;“-“)letnum=num1-num2;Break;;“\*”)letnum=num1*num2Break;;“/”)letnum=num1/num2Break;;EsacDoneEcho$num六、实验总结
本文标题:linux的shell脚本实验报告
链接地址:https://www.777doc.com/doc-5710426 .html