1、中南大学linux程序设计环境实验三实验3 Linux Shell程序设计1、实验目的(1) 掌握Linux shell程序运行方法 (2) 掌握Linux Shell程序基本语法(3) 了解Linux环境变量(4) 编写简单Linux shell程序 2、实验内容 完成简单CD唱片管理系统。(1) 需求假设你收集了大量的CD唱片,现在为了方便管理,需要设计和实现一个管理CD唱片的程序。程序至少能够把每张CD唱片的资料保存起来,如唱片的名称、音乐类型、艺术家或作曲家的名字等;然后针对某张CD唱片而言,还需要保存一些简单的曲目信息;程序要可以以CD唱片为单位进行搜索,而不是以曲目为单位;为了使程
2、序具备完整性,还需要实现对CD唱片资料的输入、修改和删除等功能。(2) 设计把标题信息和曲目信息分开,用两个文本文件分别来存储。每张CD唱片保存如下信息:编号、标题、曲目类型(流行、爵士等)、作曲家等。曲目只保存下面两个信息:编号和曲名。如下表所示:表1. 唱片信息唱片编号标题曲目类型作曲家CD123Cool saxjazzBixCD234Classic violinclassicBachCD345Hits99popVarious表2. 曲目信息目录编号曲目编号曲名CD1231Some jazzCD1232More jazzCD2341Sonata in D minorCD3451Dizzy各
3、项信息用逗号来分隔。(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 Centra
4、l 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 |
5、 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) Up
6、date 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 ent
7、er 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 c
8、dtrack=$(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 wa
9、nt 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 a
10、sklist=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
11、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,
12、 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 “$cdcatnu
13、m,” $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 $c
14、dtitle 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 $
15、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; esacdone#Tidy up and leaverm -f $temp_fileecho Finished exit 03、实验记录 4、心得体会老师提供的源码里有许多错误,通过对需求的理解以及脚本的分析,得出正确的脚本代码,比如,对$1(数字)的含义不理解,就会把它当成$l(字母),$1(数字)在shell中称为“位置参数”,表示传入的第1个参数,而$l(字母)几乎不存在,除了l(字母)是个变量外。其它类似的还有许多,所以要理解具体语句的含义后才会知道日后怎样去用。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1