Linux程序设计大报告Word格式文档下载.docx
《Linux程序设计大报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Linux程序设计大报告Word格式文档下载.docx(31页珍藏版)》请在冰豆网上搜索。
5.
6.#Thisprogramisfreesoftware;
youcanredistributeitand/ormodifyit
7.#underthetermsoftheGNUGeneralPublicLicenseaspublishedbythe
8.#FreeSoftwareFoundation;
eitherversion2oftheLicense,or(atyour
9.#option)anylaterversion.
10.
11.#Thisprogramisdistributedinthehopesthatitwillbeuseful,but
12.#WITHOUTANYWARRANTY;
withouteventheimpliedwarrantyof
13.#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNUGeneral
14.#PublicLicenseformoredetails.
15.
16.#YoushouldhavereceivedacopyoftheGNUGeneralPublicLicensealong
17.#withthisprogram;
ifnot,writetotheFreeSoftwareFoundation,Inc.
18.#675MassAve,Cambridge,MA02139,USA.
19.
20.#Thefirstthingtodoistoensurethatsomeglobalvariablesthatwe'
llbeusing
21.#throughoutthescriptaresetup.Wesetthetitleandtrackfilesandatemporaryfile.
22.#WealsotrapCtrl-C,soourtemporaryfileisremovediftheuserinterruptsthescript.
23.
24.menu_choice="
"
25.current_cd="
26.title_file="
title.cdb"
27.tracks_file="
tracks.cdb"
28.temp_file=/tmp/cdb.$$
29.trap'
rm-f$temp_file'
EXIT
30.
31.#Nowwedefineourfunctions,sothatthescript,executingfromthetopline,canfind
32.#allthefunctiondefinitionsbeforeweattempttocallanyofthemforthefirsttime.
33.#Toavoidrewritingthesamecodeinseveralplaces,thefirsttwofunctionsaresimple
34.#utilities.
35.
36.get_return(){
37.echo-e"
Pressreturn\c"
38.readx
39.return0
40.}
41.
42.get_confirm(){
43.echo-e"
Areyousure?
\c"
44.whiletrue
45.do
46.readx
47.case"
$x"
in
48.y|yes|Y|Yes|YES)
49.return0;
;
50.n|no|N|No|NO)
51.echo
52.echo"
Cancelled"
53.return1;
54.*)echo"
Pleaseenteryesorno"
;
55.esac
56.done
57.}
58.
59.#Here,wecometothemainmenufunction,set_menu_choice.
60.#Thecontentsofthemenuvarydynamically,withextraoptionsbeingaddedifaCDentry
61.#hasbeenselected.Notethatecho-emaynotbeportabletosomeshells.
62.
63.set_menu_choice(){
64.clear
65.echo"
Options:
-"
66.echo
67.echo"
a)AddnewCD"
68.echo"
f)FindCD"
69.echo"
c)CounttheCDsandtracksinthecatalog"
70.if["
$cdcatnum"
!
="
];
then
71.echo"
l)Listtrackson$cdtitle"
72.echo"
r)Remove$cdtitle"
73.echo"
u)Updatetrackinformationfor$cdtitle"
74.fi
75.echo"
q)Quit"
76.echo
77.echo-e"
Pleaseenterchoicethenpressreturn\c"
78.readmenu_choice
79.return
80.}
81.
82.#Twomoreveryshortfunctions,insert_titleandinsert_trackforaddingtothedatabasefiles.
83.#Thoughsomepeoplehateone-linerslikethese,theyhelpmakeotherfunctionsclearer
84.#Theyarefollowedbythelargeradd_record_trackfunctionthatusesthem.
85.#Thisfunctionusespatternmatchingtoensurenocommasareentered(sincewe'
reusingcommas
86.#asafieldseparator),andalsoarithmeticoperationstoincrementthecurrenttracknumber
87.#astracksareentered.
88.
89.insert_title(){
90.echo$*>
>
$title_file
91.return
92.}
93.
94.insert_track(){
95.echo$*>
$tracks_file
96.return
97.}
98.
99.add_record_tracks(){
100.echo"
EntertrackinformationforthisCD"
101.echo"
Whennomoretracksenterq"
102.cdtrack=1
103.cdttitle="
104.while["
$cdttitle"
q"
]
105.do
106.echo-e"
Track$cdtrack,tracktitle?
107.readtmp
108.cdttitle=${tmp%%,*}
109.if["
$tmp"
110.echo"
Sorry,nocommasallowed"
111.continue
112.fi
113.if[-n"
];
114.if["
115.insert_track$cdcatnum,$cdtrack,$cdttitle
116.fi
117.else
118.cdtrack=$((cdtrack-1))
119.fi
120.cdtrack=$((cdtrack+1))
121.done
122.}
123.
124.#Theadd_recordsfunctionallowsentryofthemainCDinformationforanewCD.
125.
126.add_records(){
127.#Promptfortheinitialinformation
128.
129.echo-e"
Entercatalogname\c"
130.readtmp
131.cdcatnum=${tmp%%,*}
132.
133.echo-e"
Entertitle\c"
134.readtmp
135.cdtitle=${tmp%%,*}
136.
137.echo-e"
Entertype\c"
138.readtmp
139.cdtype=${tmp%%,*}
140.
141.echo-e"
Enterartist/composer\c"
142.readtmp
143.cdac=${tmp%%,*}
144.
145.#Checkthattheywanttoentertheinformation
146.
147.echoAbouttoaddnewentry
148.echo"
$cdcatnum$cdtitle$cdtype$cdac"
149.
150.#Ifconfirmedthenappendittothetitlesfile
151.
152.ifget_confirm;
153.insert_title$cdcatnum,$cdtitle,$cdtype,$cdac
154.add_record_tracks
155.else
156.remove_records
157.fi
158.
159.return
160.}
161.
162.#Thefind_cdfunctionsearchesforthecatalognametextintheCDtitlefile,usingthe
163.#grepcommand.Weneedtoknowhowmanytimesthestringwasfound,butgreponlyreturns
164.#avaluetellingusifitmatchedzerotimesormany.Togetaroundthis,westorethe
165.#outputinafile,whichwillhaveonelinepermatch,thencountthelinesinthefile.
166.#Thewordcountcommand,wc,haswhitespaceinitsoutput,separatingthenumberoflines,
167.#wordsandcharactersinthefile.Weusethe$(wc-l$temp_file)notationtoextractthe
168.#firstparameterfromtheoutputtosetthelinesfoundvariable.Ifwewantedanother,
169.#laterparameterwewouldusethesetcommandtosettheshell'
sparametervariablesto
170.#thecommandoutput.
171.#WechangetheIFS(InternalFieldSeparator)toa,(comma),sowecanseparatethe
172.#comma-delimitedfields.Analternativecommandiscut.
173.
174.find_cd(){
175.if["
$1"
="
n"
176.asklist=n
177.else
178.asklist=y
179.fi
180.cdcatnum="
181.echo-e"
EnterastringtosearchforintheCDtitles\c"
182.readsearchstr
183.if["
$searchstr"
184.return0
185.fi
186.
187.grep"
$title_file>
$temp_file
188.
189.set$(wc-l$temp_file)
190.linesfound=$1
191.
192.case"
$linesfound"
193.0)echo"
Sorry,nothingfound"
194.get_return
195.return0
196.;
197.1);
198.2)echo"
Sorry,notunique."
199.echo"
Foundthefollowing"
200.cat$temp_file
201.get_return
202.return0
203.esac
204.
205.IFS="
"
206.readcdcatnumcdtitlecdtypecdac<
207.IFS="
"
208.
209.if[-z"
210.echo"
Sorry,couldnotextractcatalogfieldfrom$temp_file"
211.get_return
212.return0
213.fi
214.
215.echo
216.echoCatalognumber:
$cdcatnum
217.echoTitle:
$cdtitle
218.echoType:
$cdtype
219.echoArtist/Composer:
$cdac
220.echo
221.get_return
222.
223.if["
$asklist"
y"
224.echo-e"
ViewtracksforthisCD?
225.readx
226.if["
227.echo
228.list_tracks
229.echo
230.fi
231.fi
232.return1
233.}
234.
235.#update_cdallowsustore-enterinformationforaCD.Noticethatwesearch(grep)
236.#forlinesthatstart(^)withthe$cdcatnumfollowedbya,,andthatweneedtowrap
237.#theexpansionof$cdcatnumin{}sowecansearchfora,withnowhitespacebetween
238.#itandthecataloguenumber.Thisfunctionalsouses{}toenclosemultiplestatements
239.#tobeexecutedifget_confirmreturnstrue.
240.
241.update_cd(){
242.if[-z"
243.echo"
YoumustselectaCDfirst"
244.find_cdn
245.fi
246.if[-n"
247.echo"
Currenttracksare:
248.list_tracks
249.echo
250.echo"
Thiswillre-enterthetracksfor$cdtitle"
251.get_confirm&
&
{
252.grep-v"
^${cdcatnum},"
$tracks_file>
253.mv$temp_file$tracks_file
254.echo
255.add_record_tracks
256.}
257.fi
258.return
259.}
260.
261.#count_cdsgivesusaquickcountofthecontentsofourdatabase.
262.
263.count_cds(){
264.set$(wc-l$title_file)
265.num_titles=$1
266.set$(wc-l$tracks_file)
267.num_tracks=$1
268.echofound$num_titlesCDs,withatotalof$num_trackstracks
269.get_return
270.return
271.}
272.
273.#remove_recordsstripsentriesfromthedatabasefiles,usinggrep-vtoremoveall
274.#matchingstrings.Noticewemustuseatemporaryfile.
275.#Ifwetriedtodothis,
276.#grep-v"
^$cdcatnum"
>
277.#the$title_filewouldbesettoemptybythe>
outputredirectionbeforethegrep
278.#hadchancetoexecute,sogrepwouldreadfromanemptyfile.
279.
280.remove_records(){
281.if[-z"
282.echoYoumustselectaCDfirst
283.find_cdn
284.fi
285.if[-n"
286.echo"
Youareabouttodelete$cdtitle"
287.get_confirm&
288.grep-v"
289.mv$temp_file$title_file
290.grep-v"
291.mv$temp_file$tracks_file
292.cdcatnum="
293.echoEntryremoved
294.}
295.get_return
296.fi
297.return
298.}
299.
300.#List_tracksagainusesgreptoextractthelineswewant,cuttoaccessthefields
301.