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

下载本文档

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

文档简介

山西大学计算机与信息技术学院实验报告姓 名学 号专业班级计算机科学与技术课程名称操作系统实验日期2013.11.23成 绩指导教师批改日期实验名称实验三 Linux下的shell编程一、 实验目的(1) 了解shell在操作系统中的作用。(2) 理解I/O重定向和管道。(3) 学会编写简单的shell脚本程序。(4) 学会运行shell命令文件。二、实验内容 实验1 shell脚本测试 实验2 shell脚本设计三、 实验步骤与结果(1) 进一步体会shell编程的目的主要是为了管理Linux,只有将shell编程和Linux系统管理结合起来,才能真正掌握shell编程的精髓。(2) 描述各脚本的功能和输出结构,写入实验报告。输出: 功能:实现基本输入输出功能,以下程序实现了输入一个数,然后依次循环输出比此数小的数。(如,输入2,输出:1 12) 使用命令介绍:echo:命令格式:echo arg;功能:在屏幕上打印出arg指定的字符串。 bash:调用bash的形式是:bash 选择项 Shell 程序文件名;功能:Shell程序的调试主要是利用bash命令解释程序的选择项常用的选择项:-e:如果一个命令失败,就立即退出。-n:读入命令但是不执行它们。-u:置换时把未设置的变量看做出错。-v:当读入Shell输入行时把它们都显示出来。-x:执行命令时把命令和它们的参数显示出来。 运行结果: 112123 功能:输出九九乘法表 输出结果:1 x 1=11 x 2=2 2 x 2=41 x 3=3 2 x 3=6 3 x 3=91 x 4=4 2 x 4=8 3 x 4=12 4 x 4=161 x 5=5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=251 x 6=6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=361 x 7=7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=491 x 8=8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=641 x 9=9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81 计算器 功能:实现了一个简单的计算器功能,可以实现加、减、乘、除,并且输出计算结果。 使用命令介绍: read:命令格式:read 变量名。 功能:从标准输入设备读入一行,分解成若干行字,赋值给Shell程序内部定义的变量。运行结果: done.+.-.*./.qplease type your word :(e.g.1 + 2)1 + 21 + 2 = 3.+.-.*./.qplease type your word :(e.g.1 + 2)1 * 21 * 2 = 2.+.-.*./.qplease type your word :(e.g.1 + 2)q 输出当前目录下所有文件,并输出文件总数和目录总数。 功能:实现了统计当前文件下的文件和目录的总数。并且,输出统计结果。 使用命令介绍: expr 命令格式:expr argument operator argument 功能:用于整数数值计算,但也可以用于字符串操作。 输出结果:rootlocalhost # ls -al总计 336drwxr-x- 19 root root 4096 12-07 10:52 .drwxr-xr-x 24 root root 4096 12-07 08:38 .-rw- 1 root root 1464 2011-11-04 anaconda-ks.cfg-rw- 1 root root 444 12-07 10:31 .bash_history-rw-r-r- 1 root root 24 2006-07-13 .bash_logout-rw-r-r- 1 root root 191 2006-07-13 .bash_profile-rw-r-r- 1 root root 176 2006-07-13 .bashrcdrwx- 2 root root 4096 2011-11-04 .chewingdrwx- 3 root root 4096 2011-11-04 .config-rw-r-r- 1 root root 100 2006-07-13 .cshrcdrwxr-xr-x 2 root root 4096 12-07 10:31 Desktop-rw- 1 root root 26 2011-11-04 .dmrcdrwxr-x- 2 root root 4096 2011-11-04 .eggcupsdrwxr-xr-x 3 root root 4096 12-07 10:32 .emacs.d-rw- 1 root root 16 2011-11-04 .esd_authdrwx- 4 root root 4096 12-07 08:41 .gconfdrwx- 2 root root 4096 12-07 10:32 .gconfddrwxr-xr-x 3 root root 4096 2011-11-04 .gnomedrwx- 6 root root 4096 2011-11-04 .gnome2drwx- 2 root root 4096 2011-11-04 .gnome2_privatedrwxr-xr-x 2 root root 4096 2011-11-04 .gstreamer-0.10-rw-r-r- 1 root root 81 2011-11-04 .gtkrc-1.2-gnome2-rw- 1 root root 189 12-07 08:41 .ICEauthority-rw-r-r- 1 root root 38706 2011-11-04 install.log-rw-r-r- 1 root root 4366 2011-11-04 install.log.syslogdrwx- 3 root root 4096 2011-11-04 .metacitydrwxr-xr-x 3 root root 4096 2011-11-04 .nautilus-rw-r-r- 1 root root 1157 12-07 10:52 .recently-used.xbeldrwxr-xr-x 3 root root 4096 2011-11-04 .redhatdrwx- 4 root root 4096 2011-11-04 .scim-rw-r-r- 1 root root 65 12-07 10:37 shuru-rw- 1 root root 12288 12-07 10:30 .swp-rw-r-r- 1 root root 129 2006-07-13 .tcshrcdrwx- 3 root root 4096 2011-11-04 .thumbnailsdrwx- 2 root root 4096 2011-11-04 .Trash-rw-r-r- 1 root root 1761 12-07 08:42 .xsession-errors rootlocalhost # echo The number of dirctary is $dirnumThe number of dirctary is 19rootlocalhost # echo The number of file is $filenumThe number of file is 36 菜单 功能:实现了列举菜单目录,改变目录,编辑文件夹,删除文件夹,和退出程序的功能。使用命令介绍:开关语句case 条件命令格式: case 字符串 in 模式字符串 1) 若干个命令行 1;模式字符串 2)若干个命令行2;.其它命令行esac 运行结果: donelist directory.lchange directory.cedit file.eremove file.rexit menu.qlanaconda-ks.cfg Desktop install.log install.log.sysloglist directory.lchange directory.cedit file.eremove file.rexit menu.qcenter target directoryebash: cd: e: 没有那个文件或目录list directory.lchange directory.cedit file.eremove file.rexit menu.qqgoodbye 功能:将所输入的参数循环输出 输出结果:rootlocalhost # bash shuru a b c d e fa b c d e fb c d e fc d e fd e fe fF 电话本:功能:实现简单的电话本功能,即添加,查询,删除,编辑电话,最终还设置了退出功能。 运行结果: doneif you want to add a user plese type.aif you want to del a user plese type.dif you want to watch all users please type.lif you want to bianji users please type.eif you want to exit please type.qaplease type your name : 123please type your number:f you want to add a user plese type.aif you want to del a user plese type.dif you want to watch all users please type.lif you want to bianji users please type.eif you want to exit please type.ql123f you want to add a user plese type.aif you want to del a user plese type.dif you want to watch all users please type.lif you want to bianji users please type.eif you want to exit please type.q 判断当前用户的类型。 功能:显示了当前使用的虚拟机的用户名称。 运行结果:rootlocalhost # #!/bin/bashrootlocalhost # case $UID in 0) echo this is root; 0-90-9) echo this is system; 1-40-90-9) echo this is service; *) echo this is public; esacthis is root 用一个文件记录系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。功能:创建一个文件,把系统中的所有某类包名,用此文件记录。运行结果:rootlocalhost # #!/bin/bashrootlocalhost # a=/root/filerootlocalhost # if -f $a then cat $a else rpm -qa $a fi产生一个file文件:file文件中记录rpm包名: 功能:创建一个电话本,可新建联系人,删除联系人,查看联系人列表以及编辑联系人。输出结果:rootlocalhost # bash f10xinjian-1denglu-2exit-31username123passwd123456xinjian-1denglu-2exit-32username123passwd123456success(3) 完成实验二,将运行结果写入实验报告。题目:设计一个Shell脚本,完成下述功能:创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3. dir4四个目录,并将test1,test2,test3,test4四个文件分别复制到dir1,dir2,dir3. dir4相应目录下。 程序如下: rootlocalhost # #!/bin/bashrootlocalhost # for a in 1 2 3 4; do touch

温馨提示

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

评论

0/150

提交评论