最近一直在学习Shell,深感系统命令的博大精深,由其是管道和重定向更是惊叹不已。于是写了个小程序练练手:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
#CD管理系统
2
#!/bin/sh
3
#==========================
4
#定义全局变量
5
#cdinfopath:CD基本信息存储路径
6
#cdsongpath:CD歌曲存储文件路径
7
#temppath:临时文件路径
8
#pcdcode:CD代码
9
#==========================
10
cdinfopath="cdtfile.cdb"
11
cdsongpath="cdfile.cdb"
12
temppath="/tmp/cdb.$$"
13
pcdcode=""
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
trap 'rm -f $temppath echo " 用户中断程序,系统现在退出!sleep 5" ' EXIT
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
show_help()
18
{
19
echo "暂时还没有帮助文档,请见谅!"
20
get_return
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
get_return()
24
{
25
echo "请按任意键返回主菜单
."
26
read x
27
return 0
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
get_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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
#初始化
46
init()
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
#插入歌曲
63
insert_cd_songs()
64
{
65
echo $* >> $cdsongpath
66
}
67
is_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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
80
#添加歌曲
81
add_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
114
list_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
130
rmove_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
}
139
find_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
168
add_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
223
update_cd()
224
{
225
local cdcode=$@
226
rmove_cd "$cdcode"
227
add_cd
228
}
229
set_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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
237
#主调度
238
main()
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
}
282
clear
283
echo
284
echo
285
echo "Mini CD manager"
286
sleep 1
287
main
288![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
: