想不到我申请博客园已经一年了,一年中尽然一点痕迹都没有留下,莫名有冲动想开辟自己的园地,所以,就翻出以前的书,再温习下,温习过程中,也给出一些相关的测试代码吧。
下面是《linux程序设计》中关于2.8节CD唱片的综合应用部分的一些代码,使用gdialog工具,没有参考书中给的样例,按自己的想法写的,自己也是一知半解,基本上实现了上面的部分功能,当然存在很多bug,贴出来,有兴趣的也可以看看撒,写的不好,就别拍了。
====================
1 #!/bin/bash 2 #This script was created by ChudyShao in 2013,4,4 3 #This is a CD collection. 4 5 #### Initialize global variables 6 7 title_file="title.cdb" 8 tracks_file="tracks.cdb" 9 tmp_file=./cdb.$$ 10 menu_choice="" 11 trap 'rm -f $tmp_file' EXIT 12 13 ###################### Function definition 14 set_menu_choise(){ #add main menu 15 gdialog --title "Main menu" --menu "choise one" 25 100 4 1 "Check" 2 "Search" 3 "New" 4 "Exit" 2>$tmp_file 16 menu_choice=$(cat $tmp_file) #your choice 17 return 18 } 19 select_operate(){ #choise select 20 gdialog --title "select an operator" --menu "select" 25 100 5 1 "look" 2 "delete" 3 "search" 4 "add" 5 "back" 2>$tmp_file 21 menu_choice=$(cat $tmp_file) 22 case $menu_choice in 23 1) open_tracks $1;; #look 24 2) delete_content $1;; #delete 25 3) search_tracks $1;; #search 26 4) add_track $1;; #add 27 *) set_contents;; #back 28 esac 29 return 30 } 31 open_tracks(){ #check tracks 32 if [ $1 -lt 10 ];then # 10 is boundary 33 cat $tracks_file|grep CD00$1 >$tmp_file 34 else 35 cat $tracks_file|grep CD0$1 >$tmp_file 36 37 fi 38 gdialog --title "songs" --textbox $tmp_file 25 100 39 #select_operate 40 set_contents 41 return 42 } 43 delete_content(){ #delete the whole content, delete the title and tracks 44 if [ $1 -lt 10 ];then 45 sed -i "/CD00$1/d" $tracks_file 46 sed -i "/CD00$1/d" $title_file 47 LineNum=$(cat $title_file|grep -c %) 48 for((j=$1;j<=$LineNum;j++)) #此处暂未考虑9/10等特殊情况,否则要分多种情况,懒了 49 do 50 sed -e 's/CD00'$(expr $j + 1)'%/CD00'$j'%/' $title_file >$tmp_file 51 cat $tmp_file>$title_file 52 sed -e 's/CD00'$(expr $j + 1)'%/CD00'$j'%/' $tracks_file >$tmp_file 53 cat $tmp_file>$tracks_file 54 done 55 else 56 sed -i "/CD0$1/d" $tracks_file 57 sed -i "/CD0$1/d" $title_file 58 LineNum=$(cat $title_file|grep -c %) 59 for((j=$1;j<=$LineNum;j++)) 60 do 61 sed -e 's/CD0'$(expr $j + 1)'%/CD0'$j'%/' $title_file >$tmp_file 62 cat $tmp_file>$title_file 63 sed -e 's/CD0'$(expr $j + 1)'%/CD0'$j'%/' $tracks_file >$tmp_file 64 cat $tmp_file>$tracks_file 65 done 66 67 68 fi 69 70 set_contents 71 return 72 } 73 74 search_tracks(){ #first choose a special title, then search a track 75 gdialog --title "search track" --inputbox "name" 15 25 2>$tmp_file 76 searname=$(cat $tmp_file) 77 echo $searname 78 touch cdb2.$$ 79 grep $1 $tracks_file >cdb2.$$ 80 grep $searname cdb2.$$>$tmp_file 81 rm -f cdb2.$$ 82 gdialog --title "search track" --textbox $tmp_file 25 100 83 return 84 } 85 86 add_track(){ # add a new track to the title 87 gdialog --title "add tracks" --inputbox "song name" 15 25 2>$tmp_file 88 songName=$(cat $tmp_file) 89 gdialog --title "add tracks" --inputbox "writer name" 15 25 2>$tmp_file 90 writerName=$(cat $tmp_file) 91 if [ $1 -lt 10 ];then 92 insertStr="%CD00$1%$songName%$writerName%" 93 sed -i '$a\'$insertStr'' $tracks_file 94 else 95 insertStr="%CD0$1%$songName%$writerName%" 96 sed -i '$a\'$insertStr'' $tracks_file 97 fi 98 return 99 } 100 101 set_contents(){ 102 line="" 103 LineNum=$(cat $title_file|grep -c %) 104 i=1 105 while read line 106 do 107 conName[$i]="`echo $line|awk -F '%' '{print $2}'`"; 108 titleName[$i]="`echo $line|awk -F '%' '{print $3}'`" 109 typeName[$i]="`echo $line|awk -F '%' '{print $4}'`" 110 writerName[$i]="`echo $line|awk -F '%' '{print $5}'`" 111 i=$(expr $i + 1) 112 done <$title_file 113 114 strline="" 115 for((j=1;j<$(expr $LineNum + 1);j++)) 116 do 117 strline="$strline $j ${conName[j]}_${titleName[j]}_${typeName[j]}_${writerName[j]}" 118 done 119 strline="$strline $j back" 120 gdialog --title "Contents" --menu "content title type writer" 25 100 $LineNum $strline 2>$tmp_file 121 menu_choice=$(cat $tmp_file) 122 case $menu_choice in 123 $j) ;; 124 [0123456789]) select_operate $menu_choice;; #open_tracks $menu_choice;; 125 esac 126 return 127 } 128 ############# Main 129 #Welcome 130 if [ ! -f $title_file ];then 131 gdialog --title "Welcome!" --msgbox "Welcome to Chudy's CD collection! 132 This is the first time you use it" 15 25 133 clear 134 touch $title_file 135 fi 136 if [ ! -f $tracks_file ];then 137 touch $tracks_file 138 fi 139 140 # 141 quit=N 142 while [ "$quit" != "Y" ]; 143 do 144 set_menu_choise 145 case $menu_choice in 146 1) set_contents;; 147 2) ;; #whole search, refer to search_tracks 148 3) ;; #new content/tracks, refer to add_tracks 149 4) quit=Y;; 150 *) break;; 151 esac 152 done 153 rm -f $tmp_file 154 clear 155 echo "You quit this app" 156 exit 0
一些东西还是能用的,之前对sed/awk等不了解,现在有一些了解了,具体可以参考其他资料,哈哈,就算开始学习了吧。