《Beginning Linux Programming》读书笔记(二)

2Shell编程没什么多说的,记录个代码,本章最后的那个简单的CD管理程序

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> # !/ bin / sh

#Verysimpleexampleshellscript
for managingaCDcollection.
#Copyright(C)
1996 - 99 WroxPress.

#Thisprogram
is freesoftware;youcanredistributeitand / ormodifyit
#underthetermsoftheGNUGeneralPublicLicense
as publishedbythe
#FreeSoftwareFoundation;eitherversion
2 oftheLicense,or(atyour
#option)anylaterversion.

#Thisprogram
is distributed in thehopesthatitwillbeuseful,but
#WITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
#MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNUGeneral
#PublicLicense
for moredetails.

#YoushouldhavereceivedacopyoftheGNUGeneralPublicLicensealong
#with
this program; if not,writetotheFreeSoftwareFoundation,Inc.
#
675 MassAve,Cambridge,MA 02139 ,USA.

#Thefirstthingto
do is toensurethatsome global variablesthatwe ' llbeusing
#throughoutthescriptare set up.We set thetitleandtrackfilesandatemporaryfile.
#WealsotrapCtrl
- C,soourtemporaryfile is removed if theuserinterruptsthescript.

menu_choice
= ""
current_cd
= ""
title_file
= " title.cdb "
tracks_file
= " tracks.cdb "
temp_file
=/ tmp / cdb.$$
trap
' rm-f$temp_file ' EXIT

#Nowwedefineourfunctions,sothatthescript,executingfromthetopline,canfind
#allthefunctiondefinitionsbeforeweattempttocallanyofthem
for thefirsttime.
#Toavoidrewritingthesamecode
in severalplaces,thefirsttwofunctionsaresimple
#utilities.

get_return(){
echo
- e " Pressreturn/c "
readx
return 0
}

get_confirm(){
echo
- e " Areyousure?/c "
while true
do
readx
case " $x " in
y
| yes | Y | Yes | YES)
return 0 ;;
n
| no | N | No | NO)
echo
echo
" Cancelled "
return 1 ;;
* )echo " Pleaseenteryesorno " ;;
esac
done
}

#Here,wecometothemainmenufunction,set_menu_choice.
#Thecontentsofthemenuvarydynamically,withextraoptionsbeingadded
if aCDentry
#hasbeenselected.Notethatecho
- emaynotbeportabletosomeshells.

set_menu_choice(){
clear
echo
" Options:- "
echo
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
echo
- e " Pleaseenterchoicethenpressreturn/c "
readmenu_choice
return
}

#Twomorevery
short functions,insert_titleandinsert_track for addingtothedatabasefiles.
#Thoughsomepeoplehateone
- linerslikethese,theyhelpmakeotherfunctionsclearer
#Theyarefollowedbythelargeradd_record_trackfunctionthatusesthem.
#Thisfunctionusespatternmatchingtoensurenocommasareentered(sincewe
' reusingcommas
# as afieldseparator),andalsoarithmeticoperationstoincrementthecurrenttracknumber
#
as tracksareentered.

insert_title(){
echo$
* >> $title_file
return
}

insert_track(){
echo$
* >> $tracks_file
return
}

add_record_tracks(){
echo
" EntertrackinformationforthisCD "
echo
" Whennomoretracksenterq "
cdtrack
= 1
cdttitle
= ""
while [ " $cdttitle " != " q " ]
do
echo
- e " Track$cdtrack,tracktitle?/c "
readtmp
cdttitle
= ${tmp %% , * }
if [ " $tmp " != " $cdttitle " ];then
echo
" Sorry,nocommasallowed "
continue
fi
if [ - n " $cdttitle " ];then
if [ " $cdttitle " != " q " ];then
insert_track$cdcatnum,$cdtrack,$cdttitle
fi
else
cdtrack
= $((cdtrack - 1 ))
fi
cdtrack
= $((cdtrack + 1 ))
done
}

#Theadd_recordsfunctionallowsentryofthemainCDinformation
for a new CD.

add_records(){
#Prompt
for theinitialinformation

echo
- e " Entercatalogname/c "
readtmp
cdcatnum
= ${tmp %% , * }

echo
- e " Entertitle/c "
readtmp
cdtitle
= ${tmp %% , * }

echo
- e " Entertype/c "
readtmp
cdtype
= ${tmp %% , * }

echo
- e " Enterartist/composer/c "
readtmp
cdac
= ${tmp %% , * }

#Checkthattheywanttoentertheinformation

echoAbouttoadd
new entry
echo
" $cdcatnum$cdtitle$cdtype$cdac "

#Ifconfirmedthenappendittothetitlesfile

if get_confirm;then
insert_title$cdcatnum,$cdtitle,$cdtype,$cdac
add_record_tracks
else
remove_records
fi

return
}

#Thefind_cdfunctionsearches
for thecatalognametext in theCDtitlefile, using the
#grepcommand.Weneedtoknowhowmanytimesthe
string wasfound,butgreponlyreturns
#avaluetellingus
if itmatchedzerotimesormany.To get around this ,westorethe
#output
in afile,whichwillhaveonelinepermatch,thencountthelines in thefile.
#Thewordcountcommand,wc,haswhitespace
in itsoutput,separatingthenumberoflines,
#wordsandcharacters
in thefile.Weusethe$(wc - l$temp_file)notationtoextractthe
#firstparameterfromtheoutputto
set thelinesfoundvariable.Ifwewantedanother,
#laterparameterwewouldusethe
set commandto set theshell ' sparametervariablesto
#thecommandoutput.
#WechangetheIFS(InternalFieldSeparator)toa,(comma),sowecanseparatethe
#comma
- delimitedfields.Analternativecommand is cut.

find_cd(){
if [ " $1 " = " n " ];then
asklist
= n
else
asklist
= y
fi
cdcatnum
= ""
echo
- e " EnterastringtosearchforintheCDtitles/c "
readsearchstr
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,nothingfound "
get_return
return 0
;;
1 );;
2 )echo " Sorry,notunique. "
echo
" Foundthefollowing "
cat$temp_file
get_return
return 0
esac

IFS
= " , "
readcdcatnumcdtitlecdtypecdac
< $temp_file
IFS
= " "

if [ - z " $cdcatnum " ];then
echo
" Sorry,couldnotextractcatalogfieldfrom$temp_file "
get_return
return 0
fi

echo
echoCatalognumber:$cdcatnum
echoTitle:$cdtitle
echoType:$cdtype
echoArtist
/ Composer:$cdac
echo
get_return

if [ " $asklist " = " y " ];then
echo
- e " ViewtracksforthisCD?/c "
readx
if [ " $x " = " y " ];then
echo
list_tracks
echo
fi
fi
return 1
}

#update_cdallowsustore
- enterinformation for aCD.Noticethatwesearch(grep)
#
for linesthatstart( ^ )withthe$cdcatnumfollowedbya,,andthatweneedtowrap
#theexpansionof$cdcatnum
in {}sowecansearch for a,withnowhitespacebetween
#itandthecataloguenumber.Thisfunctionalsouses{}toenclosemultiplestatements
#tobeexecuted
if get_confirmreturns true .

update_cd(){
if [ - z " $cdcatnum " ];then
echo
" YoumustselectaCDfirst "
find_cdn
fi
if [ - n " $cdcatnum " ];then
echo
" Currenttracksare:- "
list_tracks
echo
echo
" Thiswillre-enterthetracksfor$cdtitle "
get_confirm
&& {
grep
- v " ^${cdcatnum}, " $tracks_file > $temp_file
mv$temp_file$tracks_file
echo
add_record_tracks
}
fi
return
}

#count_cdsgivesusaquickcountofthecontentsofourdatabase.

count_cds(){
set $(wc - l$title_file)
num_titles
= $ 1
set $(wc - l$tracks_file)
num_tracks
= $ 1
echofound$num_titlesCDs,withatotalof$num_trackstracks
get_return
return
}

#remove_recordsstripsentriesfromthedatabasefiles,
using grep - vtoremoveall
#matchingstrings.Noticewemustuseatemporaryfile.
#Ifwetriedto
do this ,
#grep
- v " ^$cdcatnum " > $title_file
#the$title_filewouldbe
set toemptybythe > outputredirectionbeforethegrep
#hadchancetoexecute,sogrepwouldreadfromanemptyfile.

remove_records(){
if [ - z " $cdcatnum " ];then
echoYoumustselectaCDfirst
find_cdn
fi
if [ - n " $cdcatnum " ];then
echo
" Youareabouttodelete$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
= ""
echoEntryremoved
}
get_return
fi
return
}

#List_tracksagainusesgreptoextractthelineswewant,cuttoaccessthefields
#wewantandthenmoretoprovideapaginatedoutput.Ifyouconsiderhowmanylines
#ofCcodeitwouldtaketore
- implementthese 20 - oddlinesofcode,you ' llappreciate
#howpowerfulatooltheshellcanbe.

list_tracks(){
if [ " $cdcatnum " = "" ];then
echonoCDselectedyet
return
else
grep
" ^${cdcatnum}, " $tracks_file > $temp_file
num_tracks
= $(wc - l$temp_file)
if [ " $num_tracks " = " 0 " ];then
echonotracksfound
for $cdtitle
else {
echo
echo
" $cdtitle:- "
echo
cut
- f 2 - - d,$temp_file
echo
}
| ${PAGER: - more}
fi
fi
get_return
return
}

#Nowallthefunctionshavebeendefined,wecanenterthemainroutine.
#Thefirstfewlinessimply
get thefilesintoaknownstate,thenwecallthemenu
#function,set_menu_choice,andactontheoutput.
#Whenquit
is selected,wedeletethetemporaryfile,writeamessageandexit
#withasuccessfulcompletioncondition.

rm
- f$temp_file
if [ ! - f$title_file];then
touch$title_file
fi
if [ ! - f$tracks_file];then
touch$tracks_file
fi

#Nowtheapplicationproper

clear
echo
echo
echo
" MiniCDmanager "
sleep
1

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)
echo
more$title_file
echo
get_return;;
q
| Q)quit = y;;
* )echo " Sorry,choicenotrecognized " ;;
esac
done

#Tidyupandleave

rm
- f$temp_file
echo
" Finished "

exit
0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值