首页 实验三 Linux下的shell编程

实验三 Linux下的shell编程

举报
开通vip

实验三 Linux下的shell编程山西大学计算机与信息技术学院 实验报告 姓 名   学 号   专业班级 计算机科学与技术 课程名称 操作系统 实验日期 2013.11.23 成 绩   指导教师   批改日期   实验名称 实验三 Linux下的Shell编程 一、 实验目的 (1) 了解shell在操作系统中的作用。 (2) 理解I/O重定向和管道。 (3) 学会编写简单的shell脚本程序。 (4) 学会运行shell命令文件。 ...

实验三  Linux下的shell编程
山西大学计算机与信息技术学院 实验 报告 软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载 姓 名   学 号   专业班级 计算机科学与技术 课程名称 操作系统 实验日期 2013.11.23 成 绩   指导教师   批改日期   实验名称 实验三 Linux下的Shell编程 一、 实验目的 (1) 了解shell在操作系统中的作用。 (2) 理解I/O重定向和管道。 (3) 学会编写简单的shell脚本程序。 (4) 学会运行shell命令文件。 二、实验内容 实验1 shell脚本测试 实验2 shell脚本 设计 领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计 三、 实验步骤与结果 (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:执行命令时把命令和它们的参数显示出来。 运行结果: 1 12 123 ② 功能:输出九九乘法表 输出结果: 1 x 1=1 1 x 2=2 2 x 2=4 1 x 3=3 2 x 3=6 3 x 3=9 1 x 4=4 2 x 4=8 3 x 4=12 4 x 4=16 1 x 5=5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25 1 x 6=6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36 1 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=49 1 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=64 1 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 变量名。 功能:从 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 输入设备读入一行,分解成若干行字,赋值给Shell程序内部定义的变量。 运行结果: > done ..............+ ..............- ..............* ............../ ..............q please type your word :(e.g.1 + 2) 1 + 2 1 + 2 = 3 ..............+ ..............- ..............* ............../ ..............q please type your word :(e.g.1 + 2) 1 * 2 1 * 2 = 2 ..............+ ..............- ..............* ............../ ..............q please type your word :(e.g.1 + 2) q ④ 输出当前目录下所有文件,并输出文件总数和目录总数。 功能:实现了统计当前文件下的文件和目录的总数。并且,输出统计结果。 使用命令介绍: expr 命令格式:expr argument operator argument 功能:用于整数数值计算,但也可以用于字符串操作。 输出结果: [root@localhost ~]# ls -al 总计 336 drwxr-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 .bashrc drwx------ 2 root root 4096 2011-11-04 .chewing drwx------ 3 root root 4096 2011-11-04 .config -rw-r--r-- 1 root root 100 2006-07-13 .cshrc drwxr-xr-x 2 root root 4096 12-07 10:31 Desktop -rw------- 1 root root 26 2011-11-04 .dmrc drwxr-x--- 2 root root 4096 2011-11-04 .eggcups drwxr-xr-x 3 root root 4096 12-07 10:32 .emacs.d -rw------- 1 root root 16 2011-11-04 .esd_auth drwx------ 4 root root 4096 12-07 08:41 .gconf drwx------ 2 root root 4096 12-07 10:32 .gconfd drwxr-xr-x 3 root root 4096 2011-11-04 .gnome drwx------ 6 root root 4096 2011-11-04 .gnome2 drwx------ 2 root root 4096 2011-11-04 .gnome2_private drwxr-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.syslog drwx------ 3 root root 4096 2011-11-04 .metacity drwxr-xr-x 3 root root 4096 2011-11-04 .nautilus -rw-r--r-- 1 root root 1157 12-07 10:52 .recently-used.xbel drwxr-xr-x 3 root root 4096 2011-11-04 .redhat drwx------ 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 .tcshrc drwx------ 3 root root 4096 2011-11-04 .thumbnails drwx------ 2 root root 4096 2011-11-04 .Trash -rw-r--r-- 1 root root 1761 12-07 08:42 .xsession-errors [root@localhost ~]# echo "The number of dirctary is $dirnum" The number of dirctary is 19 [root@localhost ~]# echo "The number of file is $filenum" The number of file is 36 ⑤ 菜单 功能:实现了列举菜单目录,改变目录,编辑文件夹,删除文件夹,和退出程序的功能。 使用命令介绍: 开关语句 case 条件命令格式: case 字符串 in 模式字符串 1) 若干个命令行 1 ;; 模式字符串 2) 若干个命令行2 ;; …. 其它命令行 esac 运行结果: > done list directory.......l change directory.....c edit file............e remove file..........r exit menu............q l anaconda-ks.cfg Desktop install.log install.log.syslog list directory.......l change directory.....c edit file............e remove file..........r exit menu............q c enter target directory e bash: cd: e: 没有那个文件或目录 list directory.......l change directory.....c edit file............e remove file..........r exit menu............q q goodbye ⑥ 功能:将所输入的参数循环输出 输出结果: [root@localhost ~]# bash shuru a b c d e f a b c d e f b c d e f c d e f d e f e f F ⑦ 电话本: 功能:实现简单的电话本功能,即添加,查询,删除,编辑电话,最终还设置了退出功 能。 运行结果: > done if you want to add a user plese type.......a if you want to del a user plese type.......d if you want to watch all users please type.l if you want to bianji users please type....e if you want to exit please type............q a please type your name : 123 please type your number: 182******** if you want to add a user plese type.......a if you want to del a user plese type.......d if you want to watch all users please type.l if you want to bianji users please type....e if you want to exit please type............q l 123 182******** if you want to add a user plese type.......a if you want to del a user plese type.......d if you want to watch all users please type.l if you want to bianji users please type....e if you want to exit please type............q ⑧ 判断当前用户的类型。 功能:显示了当前使用的虚拟机的用户名称。 运行结果: [root@localhost ~]# #!/bin/bash [root@localhost ~]# case $UID in > 0) echo this is root;; > [0-9][0-9]) echo this is system;; > [1-4][0-9][0-9]) echo this is service;; > *) echo this is public;; > esac this is root ⑨ 用一个文件 记录 混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载 系统中所有rpm包名,如果文件不存在则创建,如果存在则显示文件内容。 功能:创建一个文件,把系统中的所有某类包名,用此文件记录。 运行结果: [root@localhost ~]# #!/bin/bash [root@localhost ~]# a=/root/file [root@localhost ~]# if [ -f $a ] > then > cat $a > else > rpm -qa > $a > fi 产生一个file文件: file文件中记录rpm包名: ⑩ 功能:创建一个电话本,可新建联系人,删除联系人,查看联系人列表以及编辑联系人。 输出结果: [root@localhost ~]# bash f10 xinjian-----------------1 denglu------------------2 exit--------------------3 1 username 123 passwd 123456 xinjian-----------------1 denglu------------------2 exit--------------------3 2 username 123 passwd 123456 success (3) 完成实验二,将运行结果写入实验报告。 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 目:设计一个Shell脚本,完成下述功能:创建四个文件test1,test2,test3,test4,实现自动创建dir1,dir2,dir3. dir4四个目录,并将test1,test2,test3,test4四个文件分别复制到dir1,dir2,dir3. dir4相应目录下。 程序如下: [root@localhost ~]# #!/bin/bash [root@localhost ~]# for a in 1 2 3 4; > do > touch test$a > mkdir dir$a > cp test$a > done 运行结果: 四、 实验总结: Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。作为命令语言,它交互式的解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。 按照Shell编程的惯例,程序的第一行一般为#!/bin/bash,其中#表示该行是注释,叹号!告诉Shell运行叹号之后的命令并用文件的其余部分作为输入,也就是运行/bin/bash并让/bin/bash去执行Shell程序内容。                
本文档为【实验三 Linux下的shell编程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_321575
暂无简介~
格式:doc
大小:36KB
软件:Word
页数:12
分类:互联网
上传时间:2019-01-14
浏览量:194