shell CD唱片应用程序

出自《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 Inc

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

get_return(){
	echo -e "Press return \c"
	read x
	return 0
}
get_confirm(){
	echo -e "Are you sure? \c"
	while true
	do 
		read x
		case "$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";;
		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" != "" ]; 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(){
	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 command 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
}
add_records(){
	# Prompt for the initial information
	
	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/composer \c"
	read tmp
	cdac=${tmp%%,*}
	
	# Check that they want to enter the information
	
	echo About to add new entry
	echo "$cdcatnum $cdtitle $cdtype $cdac"
	
	# If confirmed the appen it to the titles file
	
	if get_confirm ; then
		insert_title $cdcatnum,$cdtitle,$cdtype,$cdac
		add_record_tracks
	else
		remove_records
	fi
	
	return
}

find_cd(){
	if [ "$1" = "n" ];then
		asklist=n
	else
		asklist=y
	fi
	cacatnum=""
	echo -e "Enter a string to search for in the CD title \c"
	read searchstr
	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, nothing found"
		get_return
		return 0
		;;
	1)	;;
	2)	echo "Sorry,not unique"
		echo "found the following"
		cat $temp_file
		get_return
		retrun 0
	esac
	
	IFS=","
	read cdcatnum cdtitle cdtype cdac < $temp_file
	
	IFS=" "
	
	if [ -z "$cdcatnum" ];then
		echo "Sorry.could not exact catalog field from $temp_file"
		get_return
		return 0
	fi
	
	echo
	echo Catalog number $cdcatnum
	echo Title: $cdtitle
	echo Type: $cdtype
	echo Artist/Composer: $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 && {
			grep -v "^${cdcatnum}," $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 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 "^S{cdcatnum}," $title_file > $temp_file
			mv $temp_file $title_file
			grep -v "^${cdcatnum}," $tracks_file > $temp_file
			mv $temp_file $tracks_file
			cdcatnum=""
			echo Entry removed
		}
		get_retrun
	fi
	return
}

list_tracks(){
	if [ "$cdcatnum" = "" ]; then
		echo no CD selected yet
		return
	else
		grep "^${cdcatnum}," $tracks_file > $temp_file
		num_tracks=$(wc -l $temp_file)
		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

# Now the application proper

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

#Tidy up and leave

rm -f $temp_file
echo "Finished"
exit 0
























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值