已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验编号: 四川师大LINUX程序设计实验报告 2012年10月15日计算机科学学院2010级 1班 实验名称: Shell编程试验 姓名: 杨军 学号: 2010110140 指导老师:张莹实验二 Shell编程试验1. 目的要求(1) 熟悉Shell环境和常用功能;(2) 掌握Shell基本语法结构,并能编写简单的Shell程序。(3) 独立完成Shell课程设计CD Manager2. 实验内容(1) 命令补齐功能 l 用date命令查看系统当前时间,在输入da后,按tab键,让shell自动补齐命令的后半部分。 l 用mkdir命令创建新的目录。首先输入第一个字母m,然后按tab键,由于以m开头的命令太多,shell会提示是否显示全部的可能命令,输入n。 l 再多输入一个字母k,按tab键,让shell列出以mk开头的所有命令的列表。 l 在列表中查找mkdir命令,看看还需要多输入几个字母才能确定mkdir这个命令,然后输入需要的字母,再按tab键,让shell补齐剩下的命令。 l 最后输入要创建的目录名,按回车键执行命令。 l 多试几个命令利用tab键补齐。 (2) 命令别名功能 l 输入alias命令,显示目前已经设置好的命令的别名。l 设置别名ls为ls l,以长格形式显示文件列表:alias ls=ls -l。l 显示别名ls代表的命令,确认设置生效:alias ls。 l 使用别名ls显示当前目录中的文件列表。 l 在使定义的别名不失效的情况下,使用系统的ls命令显示当前目录中的命令列表:ls。 l 删除别名ls:unalias ls。 l 显示别名ls,确认删除别名已经生效:alias ls。 l 最后再用命令ls 显示当前目录中的文件列表。 l 写出定义别名cd为cd /etc的命令 alias cd =cd /etc 。 (3) 输出重定向 l 用ls命令显示当前目录中的文件列表:ls l。 l 使用输出重定向,把ls命令在终端上显示的当前目录中的文件列表重定向到文件list中:ls l list。 l 查看文件list中的内容,注意在列表中会多出一个文件list,其长度为0。这说明shell是首先创建了一个空文件,然后再运行ls命令:cat list。 l 再次使用输出重定向,把ls命令在终端上显示的当前目录中的文件列表重定向到文件list中。这次使用管道符号进行重定向:ls l list。l 查看文件list的内容,可以看到用进行重定向是把新的输出内容附加在文件的末尾,注意其中两行list文件的信息中文件大小的区别:cat list。l 重复命令ls l list。 l 再次查看文件list中的内容,和前两次的结果相比较,注意list文件大小和创建时间的区别,完成课后第一题。 (4) 输入重定向 l 使用输入重定向,把上面生成的文件list用mail命令发送给自己:mail root list。 l 查看新邮件,看看收到的新邮件中其内容是否为list文件中的内容。 (5) 管道 l 利用管道和grep命令,在上面建立的文件list中查找字符串list:#cat list | grep list。 l 利用管道和wc命令,计算文件list中的行数、单词数和字符数:#cat list | wc。 l 查看和修改Shell变量 l 用echo命令查看环境变量PATH的值:echo $PATH。 l 设置环境变量PATH的值,把当前目录加入到命令搜索路径中去:PATH“$PATH:.”。l 用echo命令查看环境变量PATH的值:echo $PATH。 l 比较前后两次的变化。(6) 简单的shell程序l 习题1:使用Vi将程序menu.sh程序补充完善。#!/bin/bashecho Please choose either N,E,D or Q:cat $title_fileecho Please Enter Title:read titleprintf %-15s $title $title_fileecho Please Enter Type:read typeprintf %-10s $type $title_fileecho Please Enter Artist:read artistprintf %-10s $artist $title_filereturninsert_track() #利用输入重定向、系统参数在tracks.cdb中插入 $cdcatnum,$cdtrack,$cdttitle组成的记录 return# The add_record_tracks function allows entry of the main CD information for a new tracks.add_record_tracks() return 1# The add_records function allows entry of the main CD information for a new CD.add_records() return# The find_cd function searches for the catalog name text in the CD title file, using the# grep command. We need to know how many times the string was found, but grep only returns# a value telling us if it matched zero times or many. To get around this, we store the# output in a file, which will have one line per match, then count the lines in the file.# The word count command, wc, has whitespace in its output, separating the number of lines,# words and characters in the file. We use the $(wc -l $temp_file) notation to extract the# first parameter from the output to set the linesfound variable. If we wanted another,# later parameter we would use the set command to set the shells parameter variables to# the command output.# We change the IFS (Internal Field Separator) to a , (comma), so we can separate the# comma-delimited fields. An alternative command is cut.find_cd() return 1# update_cd allows us to re-enter information for a CD. Notice that we search (grep)# for lines that start () with the $cdcatnum followed by a , and that we need to wrap# the expansion of $cdcatnum in so we can search for a , with no whitespace between# it and the catalogue number. This function also uses to enclose multiple statements# to be executed if get_confirm returns true.update_cd()return# count_cds gives us a quick count of the contents of our database.count_cds() return# remove_records strips entries from the database files, using grep -v to remove all# matching strings. Notice we must use a temporary file.# If we tried to do this,# grep -v $cdcatnum $title_file# the $title_file would be set to empty by the output redirection before the grep# had chance to execute, so grep would read from an empty file.remove_records() return# List_tracks again uses grep to extract the lines we want, cut to access the fields# we want and then more to provide a paginated output. If you consider how many lines# of C code it would take to re-implement these 20-odd lines of code, youll appreciate# how powerful a tool the shell can be.list_tracks() return# Now all the functions have been defined, we can enter the main routine.# The first few lines simply get the files into a known state, then we call the menu# function, set_menu_choice, and act on the output.# When quit is selected, we delete the temporary file, write a message and exit# with a successful completion condition.rm -f $temp_fileif ! -f $title_file ; then touch $title_filefiif ! -f $tracks_file ; then touch $tracks_filefi# Now the application properclearechoechoecho Mini CD managersleep 1quit=nwhile $quit != y ;do set_menu_choice case $menu_choice in a) add_records; r) re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 17737.124-2026同轴通信电缆第1-124部分:电气试验方法漏泄电缆的耦合损耗试验
- T/ZGM 008-2025绿色产品评价 201×7凝胶型强碱性苯乙烯系阴离子交换树脂
- T/HNCAA 069-2025公路桥梁结构健康监测技术应用规程
- T/CIQA 67-2023“三同”产品 家用和类似用途电动洗衣机技术要求
- T/CAPSA 11202-2024生产安全事故应急响应标准文献元数据导则
- T/CSBME 090-2025车载医用X射线计算机体层摄影设备临床放射防护要求
- 2025~2026学年广东省河源市东源县人教版五年级上册数册中作业题【附答案】
- 2026学年湖南省醴陵市五年级语文期末提升竞赛挑战题(详细参考解析)详细答案和解析
- 2026学年云南省楚雄市五年级语文期末高分难点突破题(详细参考解析)详细答案和解析
- PHP初级面试题及详细答案(真实企业版)
- 2026国考行测真题及参考答案
- 智造未来-2026年智能建造技术与施工机器人应用全景
- 2026中国放射性废水处理用陶瓷超滤膜辐照稳定性加速测试报告
- 2026秋统编版(新教材)七年级道德与法治上册易错知识点梳理+分课填空自测练习题及答案
- 四上2-3《呼吸的变化》教学课件
- 2026年中盐榆林盐化有限公司秋季招聘笔试模拟试题及答案详解
- 2025年信息安全工程师考试真题及答案解析
- 2026年压力容器考试题库及答案
- BJS 202602 食品中伐地那非杂质30的测定
- 2026年广东省中考物理试卷附答案
- 2026年新闻记者职业资格考试新闻采编实务试卷及答案解析
评论
0/150
提交评论