




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 合肥中学清明活动方案
- 吉利售后夏季活动方案
- 吊带活动策划方案
- 名师领航活动方案
- 后勤保障活动方案
- 听力助残活动方案
- 员工亲子食堂活动方案
- 员工嘉年华活动方案
- 同业联合活动方案
- 厂里秋季运动会活动方案
- 《中药调剂技术》课件-中药调剂的概念、起源与发展
- 《数据中心节能方法》课件
- 2024年变电设备检修工(高级)技能鉴定理论考试题库-上(选择题)
- 循环系统疾病智慧树知到答案2024年哈尔滨医科大学附属第一医院
- 2024-2030年中国激光水平仪行业市场发展趋势与前景展望战略分析报告
- 部编本小学语文六年级下册毕业总复习教案
- JB∕T 11864-2014 长期堵转力矩电动机式电缆卷筒
- 小儿氨酚黄那敏颗粒的药动学研究
- 生态环境行政处罚自由裁量基准
- 长沙市开福区2024届六年级下学期小升初数学试卷含解析
- 2024年安徽普通高中学业水平选择性考试化学试题及答案
评论
0/150
提交评论