shell编程的例子(简单的CD管理代码)
来源:互联网 发布:最强围棋软件 编辑:程序博客网 时间:2024/05/22 00:29
shell编程的例子(简单的CD管理代码),作为例子学习下。
#!/bin/sh
menu_choice=""current_cd=""
title_file="title.cdb"
tracks_file="tracks.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) return 0;;
n | no | N | 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 " b) 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 $* >> $track_title
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 -e "Track $cdtrack,track title\c"
read tmp
cdtitle=${tmp%%,*}
if [ "$tmp" != "$cdtitle" ];
then
echo "Sorry,o commas allowed"
continue
fi
if [ -n "$cdtitle" ];
then
if [ "$cdtitle" != "q" ];
then
insert_track $cdcatnum,$cdtrack,$cdtitle
fi
else
cdtrack=$((cdtrack + 1))
fi
done
}
add_records(){
echo -e "Enter for the catalog name \c"
read tmp
cdcatnum=${tmp%%,*}
echo -e "Enter the title \c"
read tmp
cdtitle=${tmp%%,*}
echo -e "Enter the 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 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 ,not 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 fieled 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 list\c"
find_cd n
fi
if [ -n "$cdcatnum" ];then
echo "Current tracks is :-"
list_tracks
echo
echo "this will reenter 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_fonfirm && {
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 "Enter removed"
}
get_return
fi
return
}
list_tracks(){
if [ "cdcatnum" ];then
echo no CD selected yet
return
else
grep "^$cdcatnum," $track_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
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 ;;
u) update_cd;;
c) count_cds;;
l) list_tracks;;
b)
echo
more $title_file
echo
get_return;;
q | Q) quit=y;;
*) echo "Sorry,please choice your recognized command"
esac
done
#tidy up and leave
rm -f $temp_file
echo "Finished"
exit 0
- shell编程的例子(简单的CD管理代码)
- Shell 编程的两个简单小例子
- 初识Shell Scripts编程--最最简单的shell例子
- 简单的windows服务编程框架(MSDN例子代码)
- CD管理系统(Shell)
- shell编程例子 -- 一个简单的目录菜单
- Shell 编程简单例子
- 简单的shell scripts例子
- shell编程的小例子
- Linux程序设计(Linux shell编程的例子:统计代码行数)
- 多线程编程的简单例子
- 简单的网络编程例子
- windows shell语法及简单的例子
- 用shell简单处理文本的例子
- 自己实现简单shell的小例子
- 【Linux】shell---函数的简单例子
- 面向holder的编程,代码更清爽,管理更简单
- Linux程序设计(Linux shell编程的例子:倒计时程序)
- 通过AndroidTestCase来进行android 单元测试
- UIViewContentMode放置图片效果
- android AnimationDrawable的使用
- Ibaits初学入门
- Linux 套接字编程中的 5 个隐患
- shell编程的例子(简单的CD管理代码)
- hdu 1579 Function Run Fun
- Camera raw data directly to image using CxImage
- hdu 1010 Tempter of the Bone
- 需求说明书四要素
- scrapy如何针对不同的spider指定不同的参数
- 实现CAS与AD在Linux下集成(修订)
- rqnoj-3
- zencart后台汉化教程