- 论坛徽章:
- 0
|
- #! /bin/sh
- ####fileName:cdManage.sh
- ####简单的唱片数据库管理程序
- ####专辑资料和专辑曲目资料分开存放在两个文件中
- #### $title_file是专辑资料文件,每行包括"专辑id,专辑名称,专辑类型,艺术家"(eg:CDL1,Oasis Owns the Oasis,Rock,Oasis)
- #### $tracks_file是专辑曲目资料文件,每行包括"专辑id,专辑曲目编号,曲目名称"(eg:CDL1,1,Little by Little)
- #### 出自图灵出版社<<Linux程序设计>> P67 - P76
- menu_choice="";
- current_cd="";
- title_file="title.cdb";
- tracks_file="track.cdb";
- temp_file=/tmp/cdb.$$;
- trap 'rm -f $temp_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 "Cancelled"
- return 1;;
- *) echo "Please enter yes or no" ;;
- esac
- done
- }
- set_menu_choice()
- {
- clear;
- echo "Options:-";
- echo
- echo " a) Add new CD"
- echo " f) Print 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 infomation 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 commas 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()
- {
- 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 composer/artists \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 titles \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 uniq";
- 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 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}," $track_file > $tempfile;
- 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 number 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 > $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;
- }
- list_tracks()
- {
- if [ "$cdcatnum" = "" ];then
- echo "no CD selected yet \c"
- return;
- else
- grep "^${cdcatnum}," $tracks_file > $temp_file
- num_tracks=$(wc -l $temp_file)
- if [ $"num_tracks" = "0" ] ;then
- echo no tracks found for $cdfile
- 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
- 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
复制代码 代码可运行,在学习的过程中,楼主有些疑问想不通,测试也搞不清楚,还望各位大大赐教.
1.echo -e "enter type \c"
read tmp
这里面echo中的转义字符\c有什么作用?
我的解答:让输入停留在提示输入行,按回车后变量存入tmp中.去掉的话貌似输入不能被读取
2.用trap设置特定信号的信号处理函数的方法
trap - signalNumber
trap '' signalNumber
trap 'command to be executed' signalNumber
用户按Ctrl+C 退出时产生退出信号SIGEXIT
trap 'rm -f /tmp/songwolf/$temp_file' EXIT
3.shell脚本的调试的提示出错行数经常不对,有什么方法可以准确定位错误语句块
4.cut的用法
cut -f 2 -d, $temp_file表示什么意思?
表示:指定从$temp_file中以,为分隔符,取第二个字段(没有指定-s的情况下将把不包含分隔符的行也输出)
5.可以总结出一个用法如果$file中只有一行每行对应N个字段,字段分隔符为,则可以通过以下方法读出各字段里的值:
IFS = ","
read $var1,$var2...$varN < $file
IFS = "";
更一般地要从含N行的文件$file中分字段读出其第n行数据($file以逗号分割,N>=1, n between 1 and N)
第n行含有的记录数:
IFS = ",";
set $(head -n n $file|tail -1);
fieldNum = $?;
IFS = "";
6.上述问题用cut命令怎么解决呢?能不能对cut的用法做个介绍?
我的解答:cut类似于简单的awk函数
cut -f1,2 -d, $temp_file 通常等价于 awk -F',' 'BEGIN{OFS=","}{print $1,$2}' $temp_file
7.get_confirm定义如下:
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 "Cancelled"
return 1;;
*) echo "Please enter yes or no" ;;
esac
done
}
在主程序中有调用get_confirm函数的场景如下:
if get_confirm ; then
如果调用到get_confirm时输入'y',此时主程序中返回值为多少?怎么样才能获得这个返回值呢?是true还是false,我们可以测试一下
比如说我们在add_records中调用了get_confirm来跟用户交互是否需要增加唱片中曲目对应的记录,输入了'y',然后返回值是多少呢?
怎么样在调用主函数中获得这个数字返回值
PS:returnedStatus=$(get_confirm)这种做法不行,因为$(command)是将command命令执行结果返回给主程序 |
|