#!/bin/sh
# Very simple example shell script for managing a CD collection.
# Copyright (C) 1996-2007 Wiley Publishing Inc.
# 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.
menu_choice=""
current_cd=""
title_file="title.cdb"
tracks_file="tracks.cdb"
temp_file=/tmp/cdb.$$
trap 'rm -f $temp_file' EXIT
get_return(){
gdialog --title "" --yesno "$1" 9 18
x=$?
if [ $x = 0 ]; then
return 0
else
return 1
fi
}
get_confirm(){
gdialog --title "" --yesno "$*" 9 18
x=$?
while true
do
case $x in
0 ) exit 0
;;
* ) exit 1
esac
done
}
set_menu_choice()
{
rm -f _1.txt
if [ "cdcatnum" != "" ]; then
gdialog --title "Mini CD manager" --checklist "Options :-" 15 25 3 a "Add new CD" "on" f "Find CD" "off" c "Count the CDs and tracks in the catalog" "off" r "Remove an exist CD" "off" u "Update track information for an exist CD" "off" 2>_1.txt
x=$?
if [ $x != 0 ]; then
rm -f _1.txt
exit 1
else
menu_choice=$(cat _1.txt)
fi
else
gdialog --title "Mini CD manager" --checklist "Options :-" 15 25 3 a "Add new CD" "on" f "Find CD" "off" c "Count the CDs and tracks in the catalog" "off" 2>_1.txt
x=$?
if [ $x != 0 ]; then
rm -f _1.txt
exit 1
else
menu_choice=$(cat _1.txt)
fi
fi
rm -f _1.txt
}
insert_title()
{
echo $* >> $title_file
return
}
insert_track()
{
echo $* >> $tracks_file
return
}
add_record_tracks()
{
cdtrack=1
cdttitle=""
x=0
while [ $x = 0 ]
do
gdialog --title "Enter track information for this CD" --inputbox "Track $cdtrack, track title?" 9 30 2>_1.txt
x=$?
tmp=$(cat _1.txt)
cdttitle=${tmp%%,*}
if [ "$tmp" != "$cdttitle" ]; then
gdialog --msgbox "Sorry, no commas allowed" 9 18
continue
fi
if [ -n "$cdttitle" ]; then
insert_track $cdcatnum,$cdtrack,$cdttitle
else
cdtrack=$((cdtrack-1))
fi
cdtrack=$((cdtrack+1))
done
rm -f _1.txt
}
add_records()
{
# Prompt for the initial information
gdialog --title "" --inputbox "Enter catalog name" 9 30 2>_1.txt
x=$?
if [ $x = 1 ]; then
return
fi
tmp=$(cat _1.txt)
cdcatnum=${tmp%%,*}
rm -f _1.txt
gdialog --title "" --inputbox "Enter title" 9 30 2>_1.txt
x=$?
if [ $x = 1 ]; then
return
fi
tmp=$(cat _1.txt)
cdtitle=${tmp%%,*}
rm -f _1.txt
gdialog --title "" --inputbox "Enter type" 9 30 2>_1.txt
x=$?
if [ $x = 1 ]; then
return
fi
tmp=$(cat _1.txt)
cdtype=${tmp%%,*}
rm -f _1.txt
gdialog --title "" --inputbox "Enter artist/composer" 9 30 2>_1.txt
x=$?
if [ $x = 1 ]; then
return
fi
tmp=$(cat _1.txt)
cdac=${tmp%%,*}
rm -f _1.txt
# Check that they want to enter the information
# If confirmed then append it to the titles file
gdialog --title "" --yesno "About to add new entry $cdcatnum $cdtitle $cdtype $cdac" 9 18
x=$?
if [ $x = 0 ] ; then
insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
add_record_tracks
fi
return
}
find_cd()
{
cdcatnum=""
gdialog --title "" --inputbox "Enter a string to search for in the CD titles" 9 30 2>_1.txt
searchstr=$(cat _1.txt)
rm -f _1.txt
if [ "$searchstr" = "" ]; then
return 0
fi
grep "$searchstr" $title_file > $temp_file
set $(wc -l $temp_file)
linesfound=$1
case "$linesfound" in
0)
get_return "Sorry,nothing found"
return 0
;;
1) ;;
*)
get_return "Sorry, not unique.Found the following"
cat $temp_file > _1.txt
gdialog --title "" --textbox _1.txt 9 30
rm -f _1.txt
return 0
esac
IFS=","
read cdcatnum cdtitle cdtype cdac < $temp_file
IFS=" "
if [ -z "$cdcatnum" ];then
get_return "Sorry, could not extract catalog field from $temp_file"
return 0
fi
echo "Catalog number: $cdcatnum"'\n'"Title: $cdtitle"'\n'"echo Type: $cdtype"'\n'"Artist/Composer: $cdac" >_1.txt
gdialog --title "" --textbox _1.txt 9 30
rm -f _1.txt
gdialog --yesno "View tracks for this CD?" 9 18
x=$?
if [ $x = 0 ];then
list_tracks
fi
return 1
}
update_cd()
{
if [ -z "$cdcatnum" ];then
gdialog --msgbox "You must select a CD first" 9 18
find_cd
fi
if [ -n "$cdcatnum" ];then
gdialog --title "" --yesno "This will re-enter the tracks for $cdtitle" 9 18
if [ "$?" = 0 ];then
grep -v "^$cdcatnum," $tracks_file > $temp_file
mv $temp_file $tracks_file
add_record_tracks
fi
fi
return
}
count_cds()
{
set $(wc -l $title_file)
num_titles=$1
set $(wc -l $tracks_file)
num_tracks=$1
gdialog --infobox "found $num_titles CDs, with a total of $num_tracks tracks" 9 18
return
}
remove_records()
{
if [ -z "$cdcatnum" ];then
get_return "You must select a CD first"
find_cd
fi
if [ -n "$cdcatnum" ];then
gdialog --title "" --yesno "You are about to delete $cdtitle" 9 18
if [ "$?" = 0 ];then
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=""
get_return "Entry removed"
fi
fi
return
}
list_tracks()
{
if [ "$cdcatnum" = "" ];then
get_return "no CD selected yet"
return
else
grep "^${cdcatnum}," $tracks_file > $temp_file
num_tracks=$(wc -l $temp_file)
if [ "$num_tracks" = "" ];then
get_return "no tracks found for $cdtitle"
else
{
cut -f 2- -d , $temp_file >_1.txt
gdialog --title "$cdtitle :-" --textbox _1.txt 9 30
}
fi
fi
return
}
if [ ! -f $title_file ]; then
touch $title_file
fi
if [ ! -f $tracks_file ]; then
touch $tracks_file
fi
# Now the application proper
x1=0
while [ $x1 = 0 ];
do
set_menu_choice
x1=$?
case "$menu_choice" in
a) add_records;;
r) remove_records;;
f) find_cd;;
u) update_cd;;
c) count_cds;;
b) gdialog --textbox title_file 9 30;;
esac
done
#Tidy up and leave
rm -f temp_file
get_return "Finished"
exit 0
CD 唱片图形显示
最新推荐文章于 2021-04-11 13:57:23 发布