初学Shell
最近一直在学习Shell,深感系统命令的博大精深,由其是管道和重定向更是惊叹不已。于是写了个小程序练练手:
Code
1#CD管理系统
2#!/bin/sh
3#==========================
4#定义全局变量
5#cdinfopath:CD基本信息存储路径
6#cdsongpath:CD歌曲存储文件路径
7#temppath:临时文件路径
8#pcdcode:CD代码
9#==========================
10cdinfopath="cdtfile.cdb"
11cdsongpath="cdfile.cdb"
12temppath="/tmp/cdb.$$"
13pcdcode=""
14
15trap 'rm -f $temppath echo " 用户中断程序,系统现在退出!sleep 5" ' EXIT
16
17show_help()
18{
19 echo "暂时还没有帮助文档,请见谅!"
20 get_return
21}
22
23get_return()
24{
25 echo "请按任意键返回主菜单."
26 read x
27 return 0
28}
29
30get_confirm()
31{
32 echo "你确定要执行操作?(y/n)"
33 while true
34 do
35 read bResult
36 case "$bResult" in
37 Y | y ) return 0 ;;
38 N | n ) return 1 ;;
39 * )
40 echo "输入有误,请重试!"
41 esac
42 done
43}
44
45#初始化
46init()
47{
48 rm -rf $temppath
49 if [ ! -f $cdinfopath ] ; then
50 touch $cdinfopath
51 fi
52 if [ ! -f $cdsongpath ] ; then
53 touch $cdsongpath
54 fi
55
56}
57#插入CD基本信息
58 insert_cd_basinfo()
59{
60 echo $* >> $cdinfopath
61}
62#插入歌曲
63insert_cd_songs()
64{
65 echo $* >> $cdsongpath
66}
67is_exist_cdcode()
68{
69 local cdcode=$@
70 grep "^${cdcode%%,}" "$cdinfopath" > $temppath
71 set $(wc -l $temppath)
72 num="$1"
73 if [ "$num" -gt 0 ];then
74 return 0
75 else
76 return 1
77 fi
78}
79
80#添加歌曲
81add_songs()
82{
83 local cdcode=$@
84 local song=""
85 local songnum=1
86 clear
87 echo "添加CD$cdcode的歌曲:"
88 echo "<<返回主菜单(q\\Q)"
89 while true
90 do
91 echo "请输入第$songnum首歌曲名称:"
92 read temp
93 songname=${temp%%,}
94 if [ -z "$songname" ] || [ "$temp" != "$songname" ];then
95 echo "歌曲名称不能为空,且不能带有特殊符号逗号"
96 else
97 if [ "$songname" != "q" ] && [ "$songname" != "Q" ];then
98 songname=${songname%%,}
99 song="$cdcode,$songnum,$songname"
100 insert_cd_songs "$song"
101 songnum=$(($songnum+1))
102 else
103 break
104 fi
105 fi
106 done
107 if [ "$songnum" -gt 1 ];then
108 return 0
109 else
110 return 1
111 fi
112}
113#列出CD
114list_cd()
115{
116 local cdcode=$@
117 local cdname=""
118 local cdauthor=""
119 grep "^${cdcode%%,}" "$cdinfopath" > $temppath
120 IFS=","
121 read cdcode cdname cdauthor < "$temppath"
122 IFS=" "
123 echo "CD代码:$cdcode, CD名称:$cdname, CD作者:$cdauthor"
124 grep "^${cdcode%%,}" "$cdsongpath" > $temppath
125 echo "歌曲列表:"
126 cut -f 2- -s -d , "$temppath"
127 get_return
128}
129#删除CD
130rmove_cd()
131{
132 local cdcode=$@
133 grep -v "^${cdcode%%,}" "$cdinfopath" > $temppath
134 mv -f $temppath $cdinfopath
135 grep -v "^${cdcode%%,}" "$cdsongpath" > $temppath
136 mv -f $temppath $cdsongpath
137 echo "成功删除CD:$cdcode"
138}
139find_cd()
140{
141 local cdcode=""
142 while true
143 do
144 # clear
145 echo "请输入CD代码:"
146 echo "<<返回主菜单(q\\Q)"
147 read temp
148 cdcode=${temp%%,}
149 if [ -z "$cdcode" ] ; then
150 echo "CD代码不能为空!请重新输入!"
151 else
152 if [ "$cdcode" = "q" ] || [ "$cdcode" = "Q" ];then
153 break
154 else
155 if is_exist_cdcode $cdcode;then
156 pcdcode="$cdcode"
157 return 0
158 else
159 pcdcode=""
160 echo "没有查找到相关CD!"
161 fi
162 fi
163 fi
164 done
165 return 1
166}
167#添加CD
168add_cd()
169{
170 clear
171 local cdcode="" #CD代码
172 local cdname="" #CD名称
173 local cdauthor="" #CD作者
174 echo "添加CD:"
175 while true
176 do
177 echo "请输入CD代码:"
178 read temp
179 cdcode=${temp%%,}
180 if [ -z "$cdcode" ];then
181 echo "CD代码不能为空!请重新输入!"
182 else
183 if is_exist_cdcode "$cdcode" ;then
184 echo "CD代码:$cdcode已经存在,请重新输入!"
185 else
186 break
187 fi
188 fi
189 done
190
191 echo "请输入CD名称:"
192 read temp
193 cdname=${temp%%,}
194
195 echo "请输入CD作者:"
196 read temp
197 cdauthor=${temp%%,}
198
199 echo "你输入的CD基本信息:"
200 echo "代码:$cdcode,名称:$cdname,作者:$cdauthor"
201
202 if [ -n "$cdcode$cdname$cdauthor" ] ; then
203 get_confirm && {
204 local baseinfo="$cdcode,$cdname,$cdauthor"
205 #插入CD基本信息
206 insert_cd_basinfo "$baseinfo"
207 #添加歌曲
208 if ! add_songs "$cdcode" ; then #如果一首歌曲都没有添加,删除CD
209 remove_cd $cdcode
210 echo "添加CD失败!"
211 else
212 echo "成功添加CD!"
213 list_cd "$cdcode"
214 fi
215 }
216 else
217 echo "输入CD信息不能为空!系统返回主菜单!"
218 sleep 5
219 fi
220
221}
222#更新CD
223update_cd()
224{
225 local cdcode=$@
226 rmove_cd "$cdcode"
227 add_cd
228}
229set_menu_choice()
230{
231 clear
232 echo "请选择操作:"
233 echo "A ) 添加CD D ) 删除CD U ) 修改CD F ) 查找CD V ) 显示CD曲目"
234 echo "H ) 帮助 Q ) 退出"
235}
236
237#主调度
238main()
239{
240 init
241 while true
242 do
243 set_menu_choice
244 read input
245 case "$input" in
246 A | a )
247 add_cd;;
248 D | d )
249 if find_cd ;then
250 get_confirm && {
251 rmove_cd "$pcdcode"
252 pcdcode=""
253 }
254 fi
255 ;;
256 U | u )
257 if find_cd ;then
258 get_confirm && {
259 update_cd "$pcdcode"
260 pcdcode=""
261 }
262 fi
263 ;;
264 F | f | V | v)
265 if find_cd ;then
266 list_cd "$pcdcode"
267 pcdcode=""
268 fi
269 ;;
270 H | h )
271 show_help ;;
272 Q | q )
273 break;;
274 * )
275 echo "你的输入有误,请重新输入!"
276 esac
277 done
278 rm -f "$temppath"
279 echo "Finished"
280 exit 0
281}
282clear
283echo
284echo
285echo "Mini CD manager"
286sleep 1
287main
288
1#CD管理系统
2#!/bin/sh
3#==========================
4#定义全局变量
5#cdinfopath:CD基本信息存储路径
6#cdsongpath:CD歌曲存储文件路径
7#temppath:临时文件路径
8#pcdcode:CD代码
9#==========================
10cdinfopath="cdtfile.cdb"
11cdsongpath="cdfile.cdb"
12temppath="/tmp/cdb.$$"
13pcdcode=""
14
15trap 'rm -f $temppath echo " 用户中断程序,系统现在退出!sleep 5" ' EXIT
16
17show_help()
18{
19 echo "暂时还没有帮助文档,请见谅!"
20 get_return
21}
22
23get_return()
24{
25 echo "请按任意键返回主菜单."
26 read x
27 return 0
28}
29
30get_confirm()
31{
32 echo "你确定要执行操作?(y/n)"
33 while true
34 do
35 read bResult
36 case "$bResult" in
37 Y | y ) return 0 ;;
38 N | n ) return 1 ;;
39 * )
40 echo "输入有误,请重试!"
41 esac
42 done
43}
44
45#初始化
46init()
47{
48 rm -rf $temppath
49 if [ ! -f $cdinfopath ] ; then
50 touch $cdinfopath
51 fi
52 if [ ! -f $cdsongpath ] ; then
53 touch $cdsongpath
54 fi
55
56}
57#插入CD基本信息
58 insert_cd_basinfo()
59{
60 echo $* >> $cdinfopath
61}
62#插入歌曲
63insert_cd_songs()
64{
65 echo $* >> $cdsongpath
66}
67is_exist_cdcode()
68{
69 local cdcode=$@
70 grep "^${cdcode%%,}" "$cdinfopath" > $temppath
71 set $(wc -l $temppath)
72 num="$1"
73 if [ "$num" -gt 0 ];then
74 return 0
75 else
76 return 1
77 fi
78}
79
80#添加歌曲
81add_songs()
82{
83 local cdcode=$@
84 local song=""
85 local songnum=1
86 clear
87 echo "添加CD$cdcode的歌曲:"
88 echo "<<返回主菜单(q\\Q)"
89 while true
90 do
91 echo "请输入第$songnum首歌曲名称:"
92 read temp
93 songname=${temp%%,}
94 if [ -z "$songname" ] || [ "$temp" != "$songname" ];then
95 echo "歌曲名称不能为空,且不能带有特殊符号逗号"
96 else
97 if [ "$songname" != "q" ] && [ "$songname" != "Q" ];then
98 songname=${songname%%,}
99 song="$cdcode,$songnum,$songname"
100 insert_cd_songs "$song"
101 songnum=$(($songnum+1))
102 else
103 break
104 fi
105 fi
106 done
107 if [ "$songnum" -gt 1 ];then
108 return 0
109 else
110 return 1
111 fi
112}
113#列出CD
114list_cd()
115{
116 local cdcode=$@
117 local cdname=""
118 local cdauthor=""
119 grep "^${cdcode%%,}" "$cdinfopath" > $temppath
120 IFS=","
121 read cdcode cdname cdauthor < "$temppath"
122 IFS=" "
123 echo "CD代码:$cdcode, CD名称:$cdname, CD作者:$cdauthor"
124 grep "^${cdcode%%,}" "$cdsongpath" > $temppath
125 echo "歌曲列表:"
126 cut -f 2- -s -d , "$temppath"
127 get_return
128}
129#删除CD
130rmove_cd()
131{
132 local cdcode=$@
133 grep -v "^${cdcode%%,}" "$cdinfopath" > $temppath
134 mv -f $temppath $cdinfopath
135 grep -v "^${cdcode%%,}" "$cdsongpath" > $temppath
136 mv -f $temppath $cdsongpath
137 echo "成功删除CD:$cdcode"
138}
139find_cd()
140{
141 local cdcode=""
142 while true
143 do
144 # clear
145 echo "请输入CD代码:"
146 echo "<<返回主菜单(q\\Q)"
147 read temp
148 cdcode=${temp%%,}
149 if [ -z "$cdcode" ] ; then
150 echo "CD代码不能为空!请重新输入!"
151 else
152 if [ "$cdcode" = "q" ] || [ "$cdcode" = "Q" ];then
153 break
154 else
155 if is_exist_cdcode $cdcode;then
156 pcdcode="$cdcode"
157 return 0
158 else
159 pcdcode=""
160 echo "没有查找到相关CD!"
161 fi
162 fi
163 fi
164 done
165 return 1
166}
167#添加CD
168add_cd()
169{
170 clear
171 local cdcode="" #CD代码
172 local cdname="" #CD名称
173 local cdauthor="" #CD作者
174 echo "添加CD:"
175 while true
176 do
177 echo "请输入CD代码:"
178 read temp
179 cdcode=${temp%%,}
180 if [ -z "$cdcode" ];then
181 echo "CD代码不能为空!请重新输入!"
182 else
183 if is_exist_cdcode "$cdcode" ;then
184 echo "CD代码:$cdcode已经存在,请重新输入!"
185 else
186 break
187 fi
188 fi
189 done
190
191 echo "请输入CD名称:"
192 read temp
193 cdname=${temp%%,}
194
195 echo "请输入CD作者:"
196 read temp
197 cdauthor=${temp%%,}
198
199 echo "你输入的CD基本信息:"
200 echo "代码:$cdcode,名称:$cdname,作者:$cdauthor"
201
202 if [ -n "$cdcode$cdname$cdauthor" ] ; then
203 get_confirm && {
204 local baseinfo="$cdcode,$cdname,$cdauthor"
205 #插入CD基本信息
206 insert_cd_basinfo "$baseinfo"
207 #添加歌曲
208 if ! add_songs "$cdcode" ; then #如果一首歌曲都没有添加,删除CD
209 remove_cd $cdcode
210 echo "添加CD失败!"
211 else
212 echo "成功添加CD!"
213 list_cd "$cdcode"
214 fi
215 }
216 else
217 echo "输入CD信息不能为空!系统返回主菜单!"
218 sleep 5
219 fi
220
221}
222#更新CD
223update_cd()
224{
225 local cdcode=$@
226 rmove_cd "$cdcode"
227 add_cd
228}
229set_menu_choice()
230{
231 clear
232 echo "请选择操作:"
233 echo "A ) 添加CD D ) 删除CD U ) 修改CD F ) 查找CD V ) 显示CD曲目"
234 echo "H ) 帮助 Q ) 退出"
235}
236
237#主调度
238main()
239{
240 init
241 while true
242 do
243 set_menu_choice
244 read input
245 case "$input" in
246 A | a )
247 add_cd;;
248 D | d )
249 if find_cd ;then
250 get_confirm && {
251 rmove_cd "$pcdcode"
252 pcdcode=""
253 }
254 fi
255 ;;
256 U | u )
257 if find_cd ;then
258 get_confirm && {
259 update_cd "$pcdcode"
260 pcdcode=""
261 }
262 fi
263 ;;
264 F | f | V | v)
265 if find_cd ;then
266 list_cd "$pcdcode"
267 pcdcode=""
268 fi
269 ;;
270 H | h )
271 show_help ;;
272 Q | q )
273 break;;
274 * )
275 echo "你的输入有误,请重新输入!"
276 esac
277 done
278 rm -f "$temppath"
279 echo "Finished"
280 exit 0
281}
282clear
283echo
284echo
285echo "Mini CD manager"
286sleep 1
287main
288
:
posted on 2009-07-13 20:33 john.huang 阅读(270) 评论(0) 编辑 收藏 举报