最近在看linux shell方面的知识,一直没有一个比较完整的shell程序,这是《linux程序设计》第2章的shell综合应用的源码,照着敲了一遍,记录一下
#! /bin/bash
menu_choice=""
current_cd=""
#唱片文件
title_file="title.cdb"
#歌曲文件
tracks_file="tracks.cdb"
#$$进程id,保证文件名称唯一
tmp_file=/tmp/cdb.$$
#监听,当出现exit信号的时候,删除临时文件
trap 'rm -f $tmp_file' EXIT
get_return() {
echo -e "please return \c"
read x
return 0
}
#0时为真,1为假,exit 0 表示成功退出
get_confirm() {
echo -e "are you sure? \c"
while true
do
read x
case "$x" in
y | yes | Y | YES )
return 0;;
n | no | N | NO )
echo
echo "cancelled"
return 1;;
*) echo "please enter yes or no ";;
esac
done
}
#cacatnum唱片编号,歌曲是通过唱片编号关联起来的,cdtitle唱片名称
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
}
#把新歌曲写入数据库
inset_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 commas allowed"
continue
fi
# -n 字符串不为空为真
if [ -n "$cdttitle" ]; then
if [ "$cdttitle" != "q" ]; then
inset_track $cdcatnum,$cdtrack,$cdttitle
fi
else
cdtrack=$((cdtrack-1))
fi
cdtrack=$((cdtrack+1))
done
}
#添加新的唱片
add_records() {
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%%,*}
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
}
#根据名称查找一个唱片
find_cd() {
if [ "$1" = "n" ]; then
asklist=n
else
asklist=y
fi
cdcatnum=""
echo -e "enter a string to search for in the cd title \c"
read searchstr
if [ "$searchstr" = "" ]; then
return 0
fi
#通过grep把唱片数据里面的相关内容找出,再写入到$tmp_file
grep "$searchstr" $title_file > $tmp_file
#通过统计行数得到唱片数
set $(wc -l $tmp_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 $tmp_file
get_return
return 0
esac
IFS=","
read cdcatnum cdtitle cdtype cdac < $tmp_file
IFS=" "
# -z 字符串为空为真
if [ -z "$cdcatnum" ]; then
echo "sorry, could not extract catalog field from $tmp_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"
# 删除相关的歌曲,通过$tmp_file,把结果保存到数据库文件
get_confirm && {
grep -v "^${cdcatnum}," $tracks_file > $tmp_file
mv $tmp_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 "^${cdcatnum}," $title_file > $tmp_file
mv $tmp_file $title_file
grep -v "^${cdcatnum}," $tracks_file > $tmp_file
mv $tmp_file $tracks_file
cdcatnum=""
echo entry removed
}
get_return
fi
return
}
#列出一个唱片里面的所有歌曲
list_tracks() {
if [ "$cdcatnum" = "" ]; then
echo no cd selected yet
return
else
grep "^${cdcatnum}," $tracks_file > $tmp_file
num_tracks=$(wc -l $tmp_file)
if [ "$num_tracks" = "0" ]; then
echo no tracks found for $cdtitle
else {
echo
echo "$cdtitle :-"
echo
# 以','为分割符号,从第2个字段到最后
cut -f 2- -d, $tmp_file
echo
} | ${PAGER:-more}
fi
fi
get_return
return
}
rm -f $tmp_file
if [ ! -f $title_file ]; then
touch $title_file
fi
if [ ! -f $tracks_file ]; then
touch $tracks_file
fi
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 recagnized";;
esac
done
rm -f $tmp_file
echo "finished"
exit 0