shell CD唱片應用程式

來源:互聯網
上載者:User

出自《Linnux 程式設計》第二章,自已一個個地敲出來的,結論就是少了幾個s沒出來效果

#。/bin/sh 和 #。/bin/bash 是有差別的

開始直接敲入的是前者,老是輸出一些 -e,表示不懂


#!/bin/bash# very simple example shell script for managing a CD collection# Copyright (C) 1996-2007 Wiley Publishing Incmenu_choice=""current_cd=""title_file="title_cdb"tracks_file="tracks.cdb"temp_file=/tmp/cdb.$$trap 'rm -f $tmp_file' EXITget_return(){echo -e "Press return \c"read xreturn 0}get_confirm(){echo -e "Are you sure? \c"while truedo read xcase "$x" in y | yes | Y | Yes | YES ) return 0;; n | no  | N | NO | No ) echo echo "Canceled" return 1;; *) echo "Please enter yes or no";;esacdone}set_menu_choice(){clearecho "Options :-"echoecho "a) Add new CD"echo "f) find CD"echo "c) Count the CDs and tracks in the catalog"if [ "$cdcatnum" != "" ]; thenecho "l) List tracks on $cdtitle"echo "r) remove $cdtitle"echo "u) update track information for $cdtitle"fiecho "q) QUIT"echoecho -e "Please enter choice then press return \c"read menu_choicereturn}insert_title(){echo $* >> $title_filereturn}insert_track(){echo $* >> $tracks_filereturn}add_record_tracks(){echo "Enter track information for this CD"echo "When no more tracks enter q"cdtrack=1cdttitle=""while [ "$cdttitle" != "q" ]doecho -e "Track $cdtrack,track title ? \c"read tmpcdttitle=${tmp%%,*}if [ "$tmp" != "$cdttitle" ];thenecho "Sorry,no command allowed"continue;fiif [ -n "$cdttitle" ] ; thenif [ "$cdttitle" != "q" ] ; theninsert_track $cdcatnum,$cdtrack,$cdttitlefielsecdtrack=$((cdtrack-1))fi    cdtrack=$((cdtrack+1)) done}add_records(){# Prompt for the initial informationecho -e "Enter catalog name \c"read tmpcdcatnum=${tmp%%,*}echo -e "Enter title \c"read tmpcdtitle=${tmp%%,*}echo -e "Enter type \c"read tmpcdtype=${tmp%%,*}echo -e "Enter artist/composer \c"read tmpcdac=${tmp%%,*}# Check that they want to enter the informationecho About to add new entryecho "$cdcatnum $cdtitle $cdtype $cdac"# If confirmed the appen it to the titles fileif get_confirm ; theninsert_title $cdcatnum,$cdtitle,$cdtype,$cdacadd_record_trackselseremove_recordsfireturn}find_cd(){if [ "$1" = "n" ];thenasklist=nelseasklist=yficacatnum=""echo -e "Enter a string to search for in the CD title \c"read searchstrif [ "$searchstr" = "" ];thenreturn 0figrep "$searchstr" $title_file > $temp_fileset $(wc -l $temp_file)linesfound=$1case "$linesfound" in0)echo "Sorry, nothing found"get_returnreturn 0;;1);;2)echo "Sorry,not unique"echo "found the following"cat $temp_fileget_returnretrun 0esacIFS=","read cdcatnum cdtitle cdtype cdac < $temp_fileIFS=" "if [ -z "$cdcatnum" ];thenecho "Sorry.could not exact catalog field from $temp_file"get_returnreturn 0fiechoecho Catalog number $cdcatnumecho Title: $cdtitleecho Type: $cdtypeecho Artist/Composer: $cdacechoget_returnif [ "$asklist" = "y" ]; thenecho -e "View tracks for this CD ?\c"read xif [ "$x" = "y" ];thenecholist_tracksechofifireturn -1}update_cd(){if [ -z "$cdcatnum" ]; thenecho "You must select a CD first"find_cd nfiif [ -n "$cdcatnum" ]; thenecho "Current tracks are :-"list_tracksechoecho "This will re-enter the tracks for $cdtitle"get_confirm && {grep -v "^${cdcatnum}," $tracks_file > $temp_filemv $temp_file $tracks_fileechoadd_record_tracks}fireturn}count_cds(){set $(wc -l $title_file)num_titles=$1set $(wc -l $tracks_file)num_tracks=$1echo found $num_titles CDs,with a total of $num_tracks tracksget_returnreturn}remove_records(){if [ -z "$cdcatnum" ]; thenecho you must Select a CD firstfind_cd nfiif [ -n "$cdcatnum" ]; thenecho "You are about to delete $cdtitle"get_confirm && {grep -v "^S{cdcatnum}," $title_file > $temp_filemv $temp_file $title_filegrep -v "^${cdcatnum}," $tracks_file > $temp_filemv $temp_file $tracks_filecdcatnum=""echo Entry removed}get_retrunfireturn}list_tracks(){if [ "$cdcatnum" = "" ]; thenecho no CD selected yetreturnelsegrep "^${cdcatnum}," $tracks_file > $temp_filenum_tracks=$(wc -l $temp_file)if [ "$num_tracks" = "0" ]; thenecho no tracks found For $cdtitleelse {echoecho "$cdtitle :-"echocut -f 2- -d ,$temp_fileecho} | ${PAGER:-more}fifiget_returnreturn}rm -f $temp_fileif [ ! -f $title_file ];thentouch $title_filefiif [ ! -f $tracks_file ];thentouch $tracks_filefi# Now the application properclearechoechoecho "Mini CD manager"sleep 1quit=nwhile [ "$quit" != "y" ];doset_menu_choicecase "$menu_choice" ina)add_records;;r)remove_records;;f)find_cd_y;;u)update_cd;;c)count_cds;;l)list_tracks;;b)echomore $title_fileechoget_return;;q | Q )quit=y;;*)echo "Sorry.choice not recognized";;esacdone#Tidy up and leaverm -f $temp_fileecho "Finished"exit 0


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.