题目不难,主要是熟悉各种命令~
代码如下:
#!/bin/bash
temp_file="./tempfile.cdb"
title_file="./title_file.cdb"
column_file="./column_file.cdb"
choose=""
#return
get_return()
{
echo -e "Press return \c "
read x
return 0
}
#confirm
get_confirm()
{
echo "Are u sure?(y/n)"
while true;
do
read yn
case "$yn" in
y | Y )
return 0;;
n | N )
return 1;;
* ) echo "Please enter y or n";;
esac
done
}
add_records()
{
echo "Please input the record's name like this"
echo "CD123,Cool sax,Jazz,Bix"
read input
local temp=$(echo $input|egrep '^[[:alnum:]]+,[[:alpha:]]+,[[:alpha:]]+,[[:alpha:]]+$')
if [ "$temp" == "" ];then
echo "wrong name"
sleep 1
return 0;
else
cdnum=$(echo $input|cut -d ',' -f1)
cdtitle=$(echo $input|cut -d ',' -f2)
cdtype=$(echo $input|cut -d ',' -f3)
cdac=$(echo $input|cut -d ',' -f4)
fi
echo "the name is $cdnum $cdtitle $cdtype $cdac"
if get_confirm ;then
insert_title $cdnum $cdtitle $cdtype $cdac
add_records_cds
return
else
echo "you dont save the record"
return
fi
}
add_records_cds()
{
echo "Please input the record's list name like this:"
echo "Juhuatai,Qianlizhiwai,..."
read names
local k=$(echo "$names"|awk -F ',' '{print NF}')
#echo $k
declare -i j=1;
y=$((k));
#echo $y
while [ $j -le $y ];
do
insert_track $cdnum $j $(echo "$names"|cut -d ',' -f $j)
j=$j+1
done
}
find_records()
{
if [[ "$1" == "n" ]]; then
asklist="n";
else
asklist="y";
#statements
fi
echo "input a string to search for in the cd titles"
read searchstr
if [ "$searchstr" == "" ];then
return 0;
fi
grep -i "$searchstr" $title_file > $temp_file;
#将计算出来的数目的第一个参数(行的数量)赋给linesfound
set $(wc -l $temp_file)
linesfound=$1
case "$linesfound" in
0) echo "No found"
get_return
return 0;;
1) ;;
2) echo "Sorry,many results:"
cat $temp_file
get_return
return 0;;
esac
read cdnum cdtitle cdtype cdac < $temp_file
if [ -z "$cdnum" ]; then
echo "Sorry,can't extract details from $temp_file"
get_return
return 0
#statements
fi
echo
echo "CD number: $cdnum"
echo "Title: $cdtitle"
echo "Type: $cdtype"
echo "Artist: $cdac"
echo
get_return
if [[ "$asklist" == "y" ]]; then
echo "View tracks for this CD?"
read x
if [[ "$x" == "y" ]]; then
echo
list_records;
echo
#statements
fi
#statements
fi
return 1
}
rm_records()
{
find_records n;
if [[ -n "$cdnum" ]]; then
echo "Deleting CD: $cdnum $cdtitle $cdtype $cdac"
get_confirm &&{ grep -v "^$cdnum" $title_file > $temp_file
mv $temp_file $title_file
grep -v "^$cdnum" $column_file > $temp_file
mv $temp_file $column_file
cdnum=""
echo "Delete ready"
}
get_return
#statements
fi
return
}
update_records()
{
if [ "$cdnum" == "" ];then
echo "you dont choose a CD"
return ;
fi
echo "updateing the CD's songs $cdtitle"
get_confirm &&{
grep -v "^$cdnum" $column_file > $temp_file
mv $temp_file $column_file
echo
add_records_cds
}
return
}
count_records()
{
local num_cds=$(wc -l $title_file|cut -d " " -f1)
local num_songs=$(wc -l $column_file|cut -d " " -f1)
echo "the nums for the CD is : $num_cds"
echo "the nums for the Songs is : $num_songs"
}
list_records()
{
[ "$cdnum" == "" ]&&echo "no CD selected"&&return
grep "^$cdnum" $column_file > $temp_file
local num_songs=$(wc -l $temp_file)
if [[ "$num_songs" == "0" ]]; then
echo "no songs found for $cdtitle"
else
echo
echo "$cdtitle:-"
echo
cut -d " " -f 2- $temp_file
echo
#statements
fi
get_return
return
}
memu_display()
{
echo "Options: "
echo
echo " a) Add CD "
echo " f) Find CD"
echo " r )Remove CD"
echo " c )Count CDs"
if [ "$cdnum" != "" ];then
echo " l )List tracks for $cdtitle"
echo " u )Update track for $cdtitle"
fi
echo " q) Quit"
echo
read -p "Please choose which action: " choose
return
}
insert_title()
{
echo $* >> $title_file
return
}
insert_track()
{
echo $* >> $column_file
return
}
#main
rm -rf $temp_file
[ ! -f $title_file ] && touch $title_file ;
[ ! -f $column_file ] && touch $column_file ;
sleep 1
while true
do
memu_display
case "$choose" in
a )add_records;;
r )rm_records;;
f )find_records y;;
u )update_records;;
c )count_records;;
l )list_records;;
b )
echo
more $title_file
echo
get_return
;;
q|Q )
break;;
* )
echo "sorry,choice no right";;
esac
done
rm -rf $temp_file
echo "Finish~"
exit 0
原文地址:http://blog.csdn.net/liucimin/article/details/41173073