CD管理系统(Shell)

原创 2016年08月28日 20:58:07

参考《linux程序设计(第四版)》


<span style="font-size:14px;">#!/bin/bash

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

get_return()
{
	echo -n "press return: "
	read key
	return 0
}

get_confirm()
{
	echo -n "Are you sure?: "
	read key
	
	while true
	do
		case "$key" in
			Y | y | yes | YES)
				return 0;;
			N | n | no | NO)
				return 1;;
			*) echo "please enter yes or no";;
		esac
        read key
	done
}

set_menu_choice()
{
	clear
	echo "Option :-"
	echo
	echo "a) Add new CD"
	echo "f) Find CD"
	echo "c) Count the CDs and tracks in the catalog"
        echo "b) list CDs"
	if [ "$cdcatnum" != "" ]
	then
		echo "l) List tracks on $cdtitle"
		echo "r) Remove $cdtitle"
		echo "u) Update teack infomat for $cdtitle"
	fi
	echo "q) Quit"
	echo
	echo -n "Please enter choice the 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
	cdtitle=""

	while [ "$cdtitle" != "q" ]
	do
		echo -n "Track $cdtrack track title?: "
		read tmp
		cdtitle=${tmp%%,*}
		if [ "$tmp" != "$cdtitle" ]
		then
			echo "Sorry,no commas allowed"
			continue
		fi
		if [ -n "$cdtitle" ]
		then
			if [ "$cdtitle" != "q" ]
			then
				insert_track $cdcatnum,$cdtrack,$cdtitle,
			fi
		else
			cdtrack=$((cdtrack-1))
		fi
		cdtrack=$((cdtrack+1))
	done
}

remove_records()
{
	if [ -z "$cdcatnum" ]
	then
		echo "You must select CD frist"
		find_cd n
	fi
	if [ -n "$cdcatnum" ]
	then
		echo "You are about to delete $cdtitle"
		get_confirm && {
		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=""
		echo Entry removed
	}
	get_return
        fi
        return
}

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

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

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

	echo -n "Enter  rlist/comper: "
	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
}

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
}

find_cd()
{
	if [ "$1" = "n" ]
	then
		asklis=n
	else
		asklis=y
	fi

	cdcatnum=""
	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)
	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
		   return 0
        esac
	IFS=","
	read cdcatnum cdtitle cdtype cdac < $temp_file
	IFS=""

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

	echo
	echo Catalog number: $cdcatnum
	echo Title: $cdtitle
	echo Type: $cdtype
	echo cdas: $cdac
	echo

	get_return

	if [ "$asklist" = "y" ]
	then
		echo -n "View track this CD?: "
		read key
		if [ "$key" = "y" ]
		then
			list_track
			echo
	       fi
        fi
	return 1
}

update_cd()
{
        echo Updat_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
}


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

if [ ! -f $tracks_file ]
then
	touch $tracks_file
fi

clear

echo
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



 </span>


版权声明:本文为博主原创文章,承蒙转载请注明作者和出处

相关文章推荐

AJAX版CD管理系统下载

  • 2006年07月11日 15:42
  • 1.2MB
  • 下载

Linux--用shell写一个学生成绩管理系统

基本的操作 增加 删除 查询 展示全部学生信息 vim编辑保存信息的文件 遇到的问题1.增加学生信息时 增加学生信息时,我用的echo重定向到文件里,这里开始用的一个>,会导致覆盖。以前...

CD管理系统 C# SQL

  • 2009年12月23日 00:33
  • 38KB
  • 下载

CD管理系统

  • 2017年08月12日 15:21
  • 1.25MB
  • 下载

Shell学生成绩管理系统

#!/usr/bash #Output file save the students information FILE=./zhubinqiang_v1.0 #temp file TMP=./.tm...

AJAX版CD管理系统

  • 2012年05月05日 21:28
  • 1.24MB
  • 下载

ajax的cd管理系统

  • 2008年07月01日 23:37
  • 1.21MB
  • 下载

Shell程序---学生管理系统

读过大学,学过Linux的人懂得这种蛋疼的感觉.....Shell程序,蛮贴出来吧! 实验内容 Ø 作业:学生成绩管理程序 Ø 功能: 对学生成绩进行管理。要求实现数据的基本操作:学院和学生信息...

AJAX版CD管理系统

  • 2008年10月05日 16:13
  • 1.21MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CD管理系统(Shell)
举报原因:
原因补充:

(最多只允许输入30个字)