《Linux程序设计 第四版》之第二章的最后练习题
题目是编写一个CD数据库应用程序,
首先:用两个文件来保存存储的数据,一个是title_file.cdb,一个是column_file.cdb!
title_file.cdb里面保存的是唱片的基本信息:拥有如下四个字段:
CD的目录编号,标题,唱片类型,作曲家或艺术家;
column_file.cdb里面保存的是唱片的详细信息即唱片包含的歌曲信息:拥有如下三个字段:
CD的目录编号,曲目编号,歌曲名
其次:
有如下函数模块:
get_return() 返回函数
get_confirm() 是否继续操作函数
add_records() 增加CD基本信息函数
insert_title() 在title_file.cdb插入CD基本信息函数
add_records_cds() 增加CD详细信息函数
insert_track() 在column_file.cdb插入CD基本信息函数
find_records() 查找CD信息函数
rm_records() 删除CD信息函数
update_records() 更新CD详细信息函数
count_records() 统计CD数量以及歌曲数量信息函数
list_records() 列出选中CD的详细信息(歌曲)函数
memu_display() 菜单界面函数
代码中主要用到的命令:
grep; 过滤每一个行字符串
cut; 根据字段切割整列字符串
wc; 统计行数等数目
set;
read; 键盘输入
echo; 输出
>、>>、<; 输入输出重定向
rm
以及各种逻辑判断,字符串操作,管道命令使用等~
题目不难,主要是熟悉各种命令~
代码如下:
#!/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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。