1. 书籍《Linux程序设计》第四版 Neil Mattew,Richard Stones著,第二章中书中用shell写了一个CD 唱片程序,仿照它,我自己大概实现了其功能,代码如下:
文件:manageCd.sh
#!/bin/bash
# this program aims to manage CD and songs contained in the CD
# history:
# 2014-10-14 first release
# manageCd.sh
mainBoard()
{
choice=1
until [ $choice == "q" ]
do
clear
echo "options as follow:"
echo "a) Add new CD"
echo "v) View CD"
echo "d) Delete CD"
echo "f) Find cd"
echo "c) Count the CD and songs in the catalog"
echo "u) update CD"
echo "q) quit"
echo ""
read -p "please input your choice: " choice
while [ "$choice" != "a" -a "$choice" != "v" -a "$choice" != "d" -a "$choice" != "f" -a "$choice" != "c" -a "$choice" != "u" -a "$choice" != "q" ]
do
read -p "input error,please input again([a,v,d,f,c,u]):" choice
done
case $choice in
"a")
addNewCD
;;
"v")
viewCD
;;
"d")
deleteCD
;;
"f")
findCD
;;
"c")
countCDAndSongs
;;
"u")
updateCD
;;
"*")
echo "error"
;;
esac
echo -n "press to return"
read x
done
}
addNewCD()
{
title_file="title_file"
songs_file="songs_file"
read -p "Enter CD name: " name
while true
do
flag=0
for x in `awk -F , '{print $1}' $title_file`
do
if [ "$name" == "$x" ]; then
echo -n "CD name:$name has existed,please input CD name again: "
read name
flag=1
break;
fi
done
if [ "$flag" == 0 ]; then
break;
fi
done
read -p "Enter CD Title: " title
read -p "Enter CD Type: " type
read -p "Enter The artist/composer: " composer
echo "About to add new entry"
echo "$name $title $type