中南大学linux程序设计环境实验三_第1页
中南大学linux程序设计环境实验三_第2页
中南大学linux程序设计环境实验三_第3页
中南大学linux程序设计环境实验三_第4页
中南大学linux程序设计环境实验三_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

实验3 Linux Shell程序设计1、实验目的(1) 掌握Linux shell程序运行方法 (2) 掌握Linux Shell程序基本语法(3) 了解Linux环境变量(4) 编写简单Linux shell程序 2、实验内容完成简单CD唱片管理系统。(1) 需求假设你收集了大量的CD唱片,现在为了方便管理,需要设计和实现一个管理CD唱片的程序。程序至少能够把每张CD唱片的资料保存起来,如唱片的名称、音乐类型、艺术家或作曲家的名字等;然后针对某张CD唱片而言,还需要保存一些简单的曲目信息;程序要可以以CD唱片为单位进行搜索,而不是以曲目为单位;为了使程序具备完整性,还需要实现对CD唱片资料的输入、修改和删除等功能。(2) 设计把标题信息和曲目信息分开,用两个文本文件分别来存储。每张CD唱片保存如下信息:编号、标题、曲目类型(流行、爵士等)、作曲家等。曲目只保存下面两个信息:编号和曲名。如下表所示:表1. 唱片信息唱片编号标题曲目类型作曲家CD123Cool saxjazzBixCD234Classic violinclassicBachCD345Hits99popVarious表2. 曲目信息目录编号曲目编号曲名CD1231Some jazzCD1232More jazzCD2341Sonata in D minorCD3451Dizzy各项信息用逗号来分隔。(3) 实现程序共包含如下函数:get_return()get_confirm()set_menu_choice()insert_title()insert_track()add_record_tracks()add_records()find_cd()update_cd()count_cds()remove_records()list_tracks()准确无误代码:#!/bin/bash#Very simple example shell script for managing a CD collection.#Copyright (C) 2012-2013 Central South University.menu_choice=current_cd=title_file=title.cdbtracks_file=tracks.cdbtemp_file=/tmp/cdb.$trap rm -f $temp_file EXITget_return() echo -e Press return: c read x return 0get_confirm() echo -e Are you sure? c while true do read x case $x in y | yes | Y | Yes | YES ) return 0; n | no | N | No | NO ) echo echo Cancelled return 1; *) echo Please enter yes or no ; esac doneset_menu_choice() clear echo Options :- echo echo a) Add new CD echo f) Find CD echo c) Count the CDs and tracks in the catalog if $cdcatnum != ; then echo l) List tracks on $cdtitle echo r) Remove $cdtitle echo u) Update track information for $cdtitle fi echo q) Quit echo echo -e Please enter choice then press return c read menu_choice returninsert_title() echo $* $title_file returninsert_track() echo $* $tracks_file returnadd_record_tracks() echo Enter track information for this CD echo When no more tracks enter q cdtrack=1 cdtitle= while $cdtitle != q do echo -e Track $cdtrack, track title? c read tmp cdtitle=$tmp%,* if $tmp != $cdtitle ; then echo Sorry, no commas allowed continue fi if -n $cdtitle ; then if $cdtitle != q ; then insert_track $cdcatnum,$cdtrack,$cdtitle fi else cdtrack=$(cdtrack-1) fi cdtrack=$(cdtrack+1) doneadd_records() # Prompt for the initial information echo -e Enter catalog name: c read tmp cdcatnum=$tmp%,* echo -e Enter title: c read tmp cdtitle=$tmp%,* echo -e Enter type: c read tmp cdtype=$tmp%,* echo -e Enter artist/composer: c read tmp cdac=$tmp%,* # Check that they want to enter the information echo About to add new entry echo $cdcatnum $cdtitle $cdtype $cdac # If confirmed then append it to the titles file if get_confirm; then insert_title $cdcatnum,$cdtitle,$cdtype,$cdac add_record_tracks else remove_records fi returnfind_cd() if $1 = n ; then asklist=n else asklist=y fi cdcatnum= echo -e Enter a string to search for in the CD titles c read searchstr if $searchstr = ; then return 0 fi grep $searchstr $title_file $temp_file set $(wc -l $temp_file) linesfound=$1 case $linesfound in 0) echo Sorry, nothing found get_return return 0 ; 1) ; 2) echo Sorry, not unique. echo Found the following cat $temp_file get_return return 0 esac IFS=, read cdcatnum cdtitle cdtype cdac $emp_file mv $temp_file $tracks_file echo add_record_tracks fi returncount_cds() set $(wc -l $title_file) num_titles=$1 set $(wc -l $tracks_file) num_tracks=$1 echo found $num_titles CDs, with a total of $num_tracks tracks get_return returnremove_records() if -z $cdcatnum ; then echo You must select a CD first find_cd n fi if -n $cdcatnum ; then echo You are about to delete $cdtitle get_confirm & grep -v “$cdcatnum,” $title_file $temp_file mv $temp_file $title_file grep -v “$cdcatnum,” $tracks_file $temp_file mv $temp_file $tracks_file cdcatnum= echo Entry removed get_return fi returnlist_tracks() if $cdcatnum = ; then echo no CD selected yet return else grep $cdcatnum, $tracks_file $temp_file num_tracks=$(wc -l $temp_file) if $num_tracks = 0 ; then echo no tracks found for $cdtitle else echo echo $cdtitle :- echo cut -f 2- -d , $temp_file echo | $PAGER:-more fi fi get_return returnrm -f $temp_fileif ! -f $title_file ; then touch $title_filefiif ! -f $tracks_file ; then touch $tracks_filefi# Now the application properclearechoechoecho Mini CD manager sleep 1quit=nwhile $quit != y ;do set_menu_choice case $menu_choice in a) add_records; r) remove_records; f) find_cd y; u) update_cd; c) count_cds; l) list_tracks; b) echo more $title_file echo get_return; q | Q) quit=y; *) echo Sorry, choice not recognized; esacdo

温馨提示

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

评论

0/150

提交评论