sh写的CD管理器

    技术2022-06-12  40

    很简单的一个例子,仅供学习之用,嘿嘿。

    #!/bin/bash #版权信息声明 # Very simple example shell script for managing a CD collection. # Copyright (C) 2011 Xing Zhao Hunau. # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # This program is distributed in the hopes that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General # Public License for more details. # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc. # 675 Mass Ave, Cambridge, MA 02139, USA. # The first thing to do is to ensure that some global variables that we'll be using # throughout the script are set up. We set the title and track files and a temporary file. # We also trap Ctrl-C, so our temporary file is removed if the user interrupts the script. #变量定义 menu_choice="" current_cd="" #存储CD的相关信息 title_file="title.cdb" #存春每张CD的曲目信息 tracks_file="tracks.cdb" tmp_file=/tmp/cdb.$$ trap 'rm -f $tmp_file' EXIT #工具函数,提供返回功能 get_return(){ #-e 选项为不换行 echo -e "Press return /c" read x return 0 } #确认函数 get_confirm(){ echo -e "Are you sure(y/n)? /c" while true do read x case "$x" in y | yes | Y | YES ) return 0;; n | no | N | NO ) echo echo "Cancelled" return 1;; *) echo "Please input yes or no" ;; esac done } #菜单项 set_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" != "" ] #如果CD的书目不为空 then echo " l) list tracks on $cdtitle" echo " r) Remove $cdtitle" echo " u) Update track information for $cdtitle" fi echo " q) Quit" echo echo -e "Please enter choice then press return /c" read menu_choice return } insert_title(){ #S*代表所有参数 echo $* >> $title_file return } insert_track(){ echo $* >> $tracks_file return } #添加曲目信息 add_record_tracks(){ echo "Enter track information for this CD" echo "When no more tracks enter q" cdtrack=1 cdttitle="" while [ "$cdttitle" != "q" ] do echo -e "Track $cdtrack, track title? /c" read tmp cdttitle=${tmp%%,*} if [ "$tmp" != "$cdttitle" ]; then echo "Sorry, no commas allowed" 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 } #添加CD add_records(){ 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 /c" read tmp cdac=${tmp%%,*} echo About to add new entry echo "$cdcatnum,$cdtitle,$cdtype,$cdac" if get_confirm ; then insert_title $cdcatnum,$cdtitle,$cdtype,$cdac add_record_tracks else remove_records fi return } #查找CD find_cd(){ if [ "$1" = "n" ]; then asklist=n else asklist=y fi cdcatnum="" echo -e "Enter a string to search for in the CD titles /c" read searchstr if [ "$searchstr" = "" ]; then return 0 fi #将茶找到的行保存到tmp_file中 grep "$searchstr" $title_file > $tmp_file #设置参数1为tmp_file中的行的数目 set $(wc -l $tmp_file) linesfound=$1 case "$linesfound" in 0) echo "Sorry,nothing found." get_return return 0 ;; 1) ;; 2) echo "Sorry,not unique." echo "Found the following" cat $tmp_file get_return return 0 esac #设置分隔符为逗号 IFS="," read cdcatnum cdtitle cdtype cdac < $tmp_file IFS=" " if [ -z "$cdcatnum" ]; then echo "Sorry,could not extract catalog field from $tmp_file" get_return return 0 fi echo echo Catalog Number: $cdcatnum echo Title: $cdtitle echo Type: $cdtype echo Artist: $cdac echo get_return #是否列出唱片的曲目信息 if [ "$asklist" = "y" ]; then echo -e "View tracks for this CD? /c" read x if [ "$x" = "y" ]; then echo list_tracks echo fi fi return 1 } #更新 update_cd(){ if [ -z "$cdcatnum" ]; then echo "You must select a CD first" find_cd n fi if [ -n "$cdcatnum" ]; then echo "Current tracks are :-" list_tracks echo echo "This will re_enter the tracks for $cdtitle" get_confirm && { #-v 代表查找所有不匹配的行,^指向一行的开头 grep -v "^${cdcatnum}," $tracks_file > $tmp_file mv $tmp_file $tracks_file echo #换行 add_record_tracks } fi return } #统计 count_cds(){ set $(wc -l $title_file) num_titles=$1 set $(wc -l $tracks_file) num_tracks=$1 echo found $num_titles CDs,with a total of $num_tracks tracks get_return return } #删除 remove_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 > $tmp_file mv $tmp_file $title_file grep -v "^{cdcatnum}," $tracks_file > $tmp_file mv $tmp_file $tracks_file cdcatnum="" echo Enter removed } get_return fi return } #遍历曲目信息 list_tracks(){ if [ "$cdcatnum" = "" ]; then echo no CD selected yet return else grep "^${cdcatnum}," $tracks_file > $tmp_file num_tracks=$(wc -l $tmp_file) if [ "$num_tracks" = "0" ]; then echo "no tracks found for $cdtitle" else { echo echo "$cdtitle :-" echo #-f代表域 2 代表从第二个字符开始 -d,代表分隔符为逗号 cut -f 2- -d , $tmp_file echo } | ${PAGER:-more} #分页显示 fi fi get_return return } rm -f $tmp_file if [ ! -f $title_file ]; then #title_file 为空则创建,不为空则修改创建时间为当前时间 touch $title_file fi if [ ! -f $tracks_file ]; then touch $tracks_file fi clear echo echo "Mini CD manager" sleep 1 quit=n while [ "$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 get_return;; q | Q ) quit=y;; *) echo "Sorry,choice not recognized";; esac done rm -f $tmp_file echo "Finnished" exit 0


    最新回复(0)