基于 Linux Shell 的CD管理系统Word文档格式.docx

上传人:b****2 文档编号:4736867 上传时间:2023-05-04 格式:DOCX 页数:14 大小:19.30KB
下载 相关 举报
基于 Linux Shell 的CD管理系统Word文档格式.docx_第1页
第1页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第2页
第2页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第3页
第3页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第4页
第4页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第5页
第5页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第6页
第6页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第7页
第7页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第8页
第8页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第9页
第9页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第10页
第10页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第11页
第11页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第12页
第12页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第13页
第13页 / 共14页
基于 Linux Shell 的CD管理系统Word文档格式.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

基于 Linux Shell 的CD管理系统Word文档格式.docx

《基于 Linux Shell 的CD管理系统Word文档格式.docx》由会员分享,可在线阅读,更多相关《基于 Linux Shell 的CD管理系统Word文档格式.docx(14页珍藏版)》请在冰点文库上搜索。

基于 Linux Shell 的CD管理系统Word文档格式.docx

1轨迹标号

2轨迹名字

为了能够合并这两个文件,我们必须将轨迹信息与CD的其他信息相关联.为了这样做,我们会使用CD目录编号.因为这对于每一个CD来说是唯一的,这样他就会在标题文件和轨迹文件中只出现一次.

下面让我们来看一下一个简单的标题文件的例子:

目录标题类型作曲家

CD123CoolsaxJazzBix

CD234ClassicviolinClassicalBach

CD345Hits99PopVarious

他们所对应的轨迹信息如下:

目录轨迹标号标题

CD1231Somejazz

CD1232Morejazz

CD3451Dizzy

CD2341SonatainDminor

这两个文件使用目录域进行联合.在这里我们要记住,对于标题文件中的每一个实体在轨迹文件中会有多行与之对应.

我们要做的最后一件事就是如何来区分这些实体.在关系数据库中通常采用确定宽度的域,但是并不总是合适的.另一个常用的办法就是逗号,而这也是我们这里所采用的方法.

在我们下面的部分中,为了使得我们不至于迷惑,我们会使用下面的一些函数:

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()

一个CD程序

1在我们的例子程序中,第一行总是要保证这个程序要作为一个Shell脚本来执行,接下来的是一些版权信息:

#!

/bin/sh

#VerysimpleexampleshellscriptformanagingaCDcollection.

#Copyright(C)1996-2003WroxPress.

#Thisprogramisfreesoftware;

youcanredistributeitand/ormodifyit

#underthetermsoftheGNUGeneralPublicLicenseaspublishedbythe

#FreeSoftwareFoundation;

eitherversion2oftheLicense,or(atyour

#option)anylaterversion.

#Thisprogramisdistributedinthehopesthatitwillbeuseful,but

#WITHOUTANYWARRANTY;

withouteventheimpliedwarrantyof

#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNUGeneral

#PublicLicenseformoredetails.

#YoushouldhavereceivedacopyoftheGNUGeneralPublicLicensealong

#withthisprogram;

ifnot,writetotheFreeSoftwareFoundation,Inc.

#675MassAve,Cambridge,MA02139,USA.

2我们要做的第一件事就是保证我们在整个脚本中使用的全局变量已经进行设置.我们要设置标题文件,一个轨迹文件以及一个临时文件.我们同时要跟踪Ctrl+C操作,这样如果用户中断了脚本,我们可以保证删除临时文件.menu_choice=””

current_cd=””

title_file=”title.cdb”

tracks_file=”tracks.cdb”

temp_file=/tmp/cdb.$$

trap‘rm-f$temp_file’EXIT

3现在我们要定义我们需要的一些函数,这样脚本从顶行开始执行,在我们试着第一次调用这些函数时可以找到这些函数的定义.为了避免在一些重写一些相同的代码,最初的两个函数是简单的实用程序.

get_return(){

echo-e“Pressreturn\c”

readx

return0

}

get_confirm(){

echo-e“Areyousure?

\c”

whiletrue

do

case“$x”in

y|yes|Y|Yes|YES)

return0;

;

n|no|N|No|NO)

echo

echo“Cancelled”

return1;

*)echo“Pleaseenteryesorno”;

esac

done

4现在我们来定义主要的菜单函数,set_menu_choise.菜单的内容可以动态的变化,如果一个CD实体被选择可以增加一些其他的选项.

set_menu_choice(){

clear

echo“Options:

-”

echo“a)AddnewCD”

echo“f)FindCD”

echo“c)CounttheCDsandtracksinthecatalog”

if[“$cdcatnum”!

=“”];

then

echo“l)Listtrackson$cdtitle”

echo“r)Remove$cdtitle”

echo“u)Updatetrackinformationfor$cdtitle”

fi

echo“q)Quit”

echo-e“Pleaseenterchoicethenpressreturn\c”

readmenu_choice

return

5现在是两个非常短小的函数,insert_title和insert_track,这样可以增加数据库文件.虽然一些讨厌这样的内容,但是他们却可以使得其他的函数看起来更为简洁.

紧随着他们的是一个较大的函数定义,add_record_track,在这其中使用前面的两个函数.这个函数使用模式匹配从而保证用户没有输入逗号(因为我们要使用逗号作为区域分隔符),并且当输入了轨迹信息后使用算村运算来增加轨迹数.

insert_title(){

echo$*>

>

$title_file

insert_track(){

$tracks_file

add_record_tracks(){

echo“EntertrackinformationforthisCD”

echo“Whennomoretracksenterq”

cdtrack=1

cdttitle=””

while[“$cdttitle”!

=“q”]

echo-e“Track$cdtrack,tracktitle?

readtmp

cdttitle=${tmp%%,*}

if[“$tmp”!

=“$cdttitle”];

echo“Sorry,nocommasallowed”

continue

if[-n“$cdttitle”];

if[“$cdttitle”!

=“q”];

insert_track$cdcatnum,$cdtrack,$cdttitle

else

cdtrack=$((cdtrack-1))

cdtrack=$((cdtrack+1))

6add_records函数允许将一个主CD的信息实体作为一个新的CD

add_records(){

#Promptfortheinitialinformation

echo-e“Entercatalogname\c”

cdcatnum=${tmp%%,*}

echo-e“Entertitle\c”

cdtitle=${tmp%%,*}

echo-e“Entertype\c”

cdtype=${tmp%%,*}

echo-e“Enterartist/composer\c”

cdac=${tmp%%,*}

#Checkthattheywanttoentertheinformation

echoAbouttoaddnewentry

echo“$cdcatnum$cdtitle$cdtype$cdac”

#Ifconfirmedthenappendittothetitlesfile

ifget_confirm;

insert_title$cdcatnum,$cdtitle,$cdtype,$cdac

add_record_tracks

remove_records

7find_cd函数使用grep命令在CD标题文件中查找指定的目录名字的文本.我们需要知道查找到多少次字符串,但是grep命令只会在他匹配了零次或是多次时才返回一个值.因为这样的情况,我们在一个文件中存储这些输出,其中每一个行匹配一个,然后我们可以计算这个文件中的行数.

字数统计命令wc,在他的输出中使用空格来分隔行数,字数以及文件中的字符数.我们使用$(wc-l$temp_file)命令来从这个命令的输出中得到第一个参数来设置linesfound变量.如果我们希望得到其他的后面的参数,我们可以使用set命令来设置命令输出的Shell参数变量.

我们将IFS设置为逗号,这样我们就可以使用逗号来进行分隔了.另一个办法就是使用cut命令.

find_cd(){

if[“$1”=“n”];

asklist=n

asklist=y

cdcatnum=””

echo-e“EnterastringtosearchforintheCDtitles\c”

readsearchstr

if[“$searchstr”=“”];

grep“$searchstr”$title_file>

$temp_file

set$(wc-l$temp_file)

linesfound=$l

case“$linesfound”in

0)echo“Sorry,nothingfound”

get_return

;

1);

2)echo“Sorry,notunique.”

echo“Foundthefollowing”

cat$temp_file

esac

IFS=”,”

readcdcatnumcdtitlecdtypecdac$temp_file

mv$temp_file$tracks_file

}

9count_cds将会返回我们数据库内容的一个快速计数.

count_cds(){

set$(wc-l$title_file)

num_titles=$l

set$(wc-l$tracks_file)

num_tracks=$l

echofound$num_titlesCDs,withatotalof$num_trackstracks

10remove_records将断开数据库文件与实体之间的联系,使用grep-v命令来移除所有匹配的字符串.在这里我们要注意的是我们必须使用临时文件.

如果我们试着使用下面的命令:

grep-v“^$cdcatnum”>

在grep命令有机会执行之前$title_file已经使用>

输出重定向设置为一个空文件,所以grep命令将会读取一个空文件.

remove_records(){

if[-z“$cdcatnum”];

echoYoumustselectaCDfirst

find_cdn

if[-n“$cdcatnum”];

echo“Youareabouttodelete$cdtitle”

get_confirm&

&

{

grep-v“^${cdcatnum},”$title_file>

mv$temp_file$title_file

grep-v“^${cdcatnum},”$tracks_file>

echoEntryremoved

11list_tracks现一次使用grep命令来解压我们需要的行,并且截取我们所需要部分而且会提供一个已标记的输出.如果我们来考虑一下要将这些20几行的古怪代码使用C代码重新进行实现会需要多少行代码,这时我们就会体会Shell是一个多么强大的工具了.

list_tracks(){

if[“$cdcatnum”=“”];

echonoCDselectedyet

grep“^${cdcatnum},”$tracks_file>

num_tracks=$(wc-l$temp_file)

if[“$num_tracks”=“0”];

echonotracksfoundfor$cdtitle

else{

echo“$cdtitle:

cut-f2--d,$temp_file

}|${PAGER:

-more}

12现在所有的函数都已经进行定义,我们可以进入我们的主要部分了.最初的几行可以简单的将文件读入到一个已知的状态,然后我们调用菜单函数,set_menu_choice并且反应在输出.

当选择了quit,我们删除这个临时文件,输出信息,并且已成功条件返回.

rm-f$temp_file

if[!

-f$title_file];

touch$title_file

fi

-f$tracks_file];

touch$tracks_file

#Nowtheapplicationproper

clear

echo

echo“MiniCDmanager”

sleep1

quit=n

while[“$quit”!

=“y”];

do

set_menu_choice

case“$menu_choice”in

a)add_records;

r)remove_records;

f)find_cdy;

u)update_cd;

c)count_cds;

l)list_tracks;

b)

more$title_file

get_return;

q|Q)quit=y;

*)echo“Sorry,choicenotrecognized”;

done

#Tidyupandleave

echo“Finished”

exit0

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2