GreensnoWorld
记录点滴,分享乐趣,一块凝固的时间
Shell 程序设计 CD 管理应用示例
Linux  2020年12月28日
#!/bin/sh

# Very sample example shell script for managing a CD collection.

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

get_return() {
	echo -n "Press return"
	read x
	return 0
}

get_confirm() {
	echo -n "Are you sure? "
	while true
	do
		read x
		case "$x" in
			y | yes | Y | Yes | YES )
				return 0;;
			n | no | N | No | NO )
				echo "Cancelled"
				return 1;;
			*) echo "Please input yes or no";;
		esac
	done
}

set_menu_choice() {
	clear
	echo "Options :-"
	echo "	a) Add new CD"
	echo "	f) Find CD"
	echo "	c) Count the CDs and tracks in the catalog"
	echo "	b) List titles"
	if [ "$cdid" != "" ]; then
		echo "	l) List tracks on $cdtitle"
		echo "	r) Remove $cdtitle"
		echo "	u) Update track information for $cdtitle"
	fi
	echo "	q) Quit"
	echo -n "Please enter choice then press return "
	read menu_choice
	return
}

insert_title() {
	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 -n "Track $cdtrack, track title? "
		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 $cdid,$cdtrack,$cdttitle
				cdtrack=$((cdtrack+1))
			fi
		fi
	done 
} 

add_records() {
	echo -n "Enter catalog name: "
	read tmp
	cdcatname=${tmp%%,*}

	echo -n "Enter title: "
	read tmp
	cdtitle=${tmp%%,*}

	echo -n "Enter type: "
	read tmp
	cdtype=${tmp%%,*}

	echo -n "Enter artist/composer: "
	read tmp
	cdac=${tmp%%,*}

	echo "About to add new entry"
	echo "$cdcatname $cdtitle $cdtype $cdac"

	if get_confirm; then
		set $(wc -l $title_file)
		ln=$1
		if [ "$1" == "0" ]; then
			cdid=1
		else
			IFS=","
			set $(tail -n 1 $title_file)
			cdid=$(($1+1))
			IFS=" "
		fi
		insert_title $cdid,$cdcatname,$cdtitle,$cdtype,$cdac
		add_record_tracks
	else
		remove_records
	fi

	return
}

find_cd() {
	if [ "$1" = "n" ]; then
		asklist=n
	else
		asklist=y
	fi
	cdid=""
	echo -n "Enter a string to search for in the CD titles: "
	read searchstr
	if [ "$searchstr" = "" ]; then
		return 0
	fi

	grep "$searchstr" $title_file > $temp_file

	set $(wc -l $temp_file)
	linesfount=$1

	case $linesfount in
		0)	echo "Sorry, nothing found"
			get_return
			return 0
			;;
		1)	;;
		2)	echo "Sorry, not unique"
			echo "Found the following"
			cat $temp_file
			get_return
			return 0
	esac

	IFS=","
	read cdid cdcatname cdtitle cdtype cdac < $temp_file
	IFS=" "

	if [ -z "cdid" ]; then
		echo "Sorry, could not extract catalog field from $temp_file"
		get_return
		return 0
	fi

	echo
	echo "Catalog id: $cdid"
	echo "Catalog name: $cdcatname"
	echo "Title: $cdtitle"
	echo "Type: $cdtype"
	echo "Artist/Composer: $cdac"
	echo
	get_return

	if [ "$asklist" = "y" ]; then
		echo -n "View tracks for this CD? "
		read x
		if [ "$x" = "y" ]; then
			echo
			list_tracks
			echo
		fi
	fi
	return 1
}

update_cd() {
	if [ -z "$cdid" ]; then
		echo "You must select a CD first"
		find_cd n
	fi
	if [ -n "$cdid" ]; then
		echo "Current tracks are :-"
		list_tracks
		echo
		echo "This will re-enter the tracks for $cdtitle"
		get_confirm && {
			grep -v "^${cdid}," $tracks_file > $temp_file
			mv $temp_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 "fount $num_titles CDs, with a total of $num_tracks tracks"
	get_return
	return
}

remove_records() {
	if [ -z "$cdid" ]; then
		echo "You must select a CD frist"
		find_cd n
	fi
	if [ -n "$cdid" ]; then
		echo "You are about to delete $cdtitle"
		get_confirm && {
			grep -v "^${cdid}," $title_file > $temp_file
			mv $temp_file $title_file
			grep -v "^${cdid}," $tracks_file > $temp_file
			mv $temp_file $tracks_file
			cdid=""
			echo "Entry removed"
		}
	fi
	return
}

list_tracks() {
	if [ "$cdid" = "" ]; then
		echo "no CD selected yet"
		return
	else
		grep "^${cdid}," $tracks_file > $temp_file
		set $(wc -l $temp_file)
		num_tracks=$1
		if [ "$num_tracks" = "0" ]; then
			echo "no tracks found for $cdtitle"
		else {
			echo
			echo "$cdtitle :-"
			echo
			cut -f 2- -d , $temp_file
			echo
		} | ${PAGER:-more}
		fi
	fi
	get_return
	return
}

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

# Application start 
clear
echo
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
			echo
			get_return;;
		q | Q ) quit=y;;
		*) echo "Sorry, choice not recognized";;
	esac
done

rm -f $temp_file
echo "Finished"
exit 0
LIJG
余本顽劣,生于紫云下,长于汝水滨。早年求学,兴趣广泛,好高骛远,学无所成,仓皇入世。兴趣所致,投身互联网,求知未证,而立已至,始悟光阴荏苒,终需务实钻研。故有此站,记录时光,积累点滴,验证所学,分享愚见。指舞方寸间,心系万千年。
留言