实验6Linux下的shell编程_第1页
实验6Linux下的shell编程_第2页
实验6Linux下的shell编程_第3页
实验6Linux下的shell编程_第4页
实验6Linux下的shell编程_第5页
已阅读5页,还剩2页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

《Linux操作系统》实验报告(实验名称:实验六:Linux下的shell编程(二))专业计算机科学与技术班级计科一班学号202111107013学生姓名郭建杰指导老师黄萍攀枝花数计学院2023年4月13日一、实验目的综合Linux常用命令和vi编辑器的使用,熟练掌握shell脚本编程。二、实验内容(1)shell脚本的建立和执行;(2)shell分支语句的使用;(3)循环语句的使用;(4)shell编程的综合应用。三.实验软件VMWareWorkstation12.0;Ubuntu;四、实验要求1.编写一个脚本demo5_1,求斐波那契数列的前10项及总和。(使用for循环)(注:斐波那契数列指的是这样一个数列:1,1,2,3,5,8,13,21,34,55,89...)For循环:#!/bin/basha=1b=1echo-n"$a+$b"sum=2for((i=1;i<=8;i++))dotmp=`expr$a+$b`echo-n"+$tmp"((a=b))((b=tmp))sum=`expr$sum+$tmp`doneecho"=$sum"while循环:#!/bin/basha=1b=1echo-n-e"$a\t$b"let"n=a+b"count=4while[$count-gt0]dolet"a=a+b"let"b=b+a"echo-n-e"\t$a\t$b"let"n+=a+b"let"count=count-1"doneechoecho"Thesumis$n"#end2.编写一个脚本demo5_2,求一个数的逆序。(使用while循环)echo-npleaseinputnum:readnumecho-nThenumiswhile[$num-gt0]dosd=$(($num%10))echo-n"$sd"num=$(($num/10))doneecho3.用Shell编程(demo5_3),判断一文件是不是普通文件,如果是将其拷贝到/home/stu/test目录下,用命令查看test目录内容,最后再删除test目录。(使用if-else语句)#!/bin/bashmkdir-m777/home/stu/testecho-n"inputfilename:"readfilenameiftest-f"$filename"thenecho"The$filenameisacommonfile!"cp$filename/home/stu/testls/home/stu/testcdtestrm$filenameechols/home/stu/testelseecho"The$filenameisnotacommonfile!"fi4.编程实现简单计算器(demo5_4)。要求用户输入一个表达式并输入结果,程序会判断用户输入的结果是否正确,并给出提示。直到用户输入‘q’时,才退出执行。(综合使用if、case等语句)#!/bin/bashechoHello!@_@echoWelcomtothecalculatetesting!echoYoucaninputanexpretionsuchas2*2or3+1,andinputtheanswerechoIwilltellyouwhetheryouarerightorwrong.echoYoucaninput'q'toexit.echo"Nowlet'sbegin!"number1=0;while["$number1"!="q"]doechoInputthefirstnumber:readnumber1echoInputtheoperatortype:readtypeechoInputthesecondnumber:readnumber2echoInputtheanswer:readyourAnswercase$typein+)myAnswer=`expr$number1+$number2`;;-)myAnswer=`expr$number1-$number2`;;\*)myAnswer=`expr$number1\*$number2`;;/)if[$number2-eq0]thenecho"Sorry!:-("echo"0cannotbethedivisor"continueelsemyAnswer=`expr$number1/$number2`fi;;*)echoError!;;esacif[$myAnswer-eq$yourAnswer]thenecho":-)"Congratulations!echoYourareright!echoInput'q'toexitortryagain!elseecho":-("Sorry!echoYouarewrong!echo"Therightansweris:"echo"$number1$type$number2=$myAnswer"echoInput'q'to

温馨提示

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

评论

0/150

提交评论