1、shell电话本#!/bin/bash#dis: a phone book#na: phonebok.shex_menu() clearecho echo =echo Good Bye !echo = exit 0#print_all () if $1 = f then awk -F : BEGIN split(name:num:nicename:work:phone_num:local_num:birthday:e-mail:qq/msn:address:remarks,arry,:) for(i=1;i=NF;i+) if($i != ) print arryi: $i n print n
2、 system(sleep 1) ./phone.bak else cat ./phone.bak fiprint_name() if $1 = f then awk -F : print name: $1; if ($5!=) print Phone:$5 n else print Phone is Null n ./phone.bak else cut -d : -f1,5 ./phone.bak fi add_rec () read -p Name: name if $name = qqq then return 0 fi read -p Nicename: nicename read
3、-p Work: work get_pho_num () read -p Phone_number: phone_num if $#phone_num != 11 then echo Wrong phone number,try agin get_pho_num else echo $phone_num echo $phone_num | grep -E :punct:|:alpha: if $? = 0 then echo The number is wrong ,try agin get_pho_num else cut -d : -f5 ./phone.bak |grep $phone_
4、num if $? = 0 then echo The number has extesed ,try agin get_pho_num else return 0 fi fi fi get_pho_num get_loc_num() read -p Locale_nmber: local_num if $#local_num 7 then echo Wrong phone number,try agin get_loc_num fi echo $local_num | grep -E :punct:|:alpha: & echo The number is wrong ,try agin &
5、 get_loc_num get_loc_num get_bir() read -p Birthday YYYY-MM-DD: birthday if ( date -d $birthday +%Y-%m-%d) then birthday=date -d $birthday +%Y-%m-%d return 0 else echo The birthday is wrong,try agin get_bir fi get_bir get_mail() read -p E-mail: mail_a if ( echo $mail_a|grep -E .+.+.com ) then return
6、 0 else echo The E-mail is wrong,try agin get_mail fi get_mail get_qq() read -p QQ/MSN: qq if $#qq ./phone.bak first_menu() catEOF2 =FirstUserMenu= 1) Add New Records R) Go Main Menu After Add Q) Quit = EOF2 while : do echo read -n 1 -p Main Menu 1|Rr|Qq menu2 echo case $menu2 in 1) add_rec ; R|r) r
7、eturn ; Q|q) exit ;# *) echo Bad choice# ; esac done#search_spe() echo key_word=$2 echo $key_word case $1 in name) cut -d : -f1 ./phone.bak | grep $key_word | grep -f- ./phone.bak ; number) cut -d : -f5 ./phone.bak | grep $key_word | grep -f- ./phone.bak ; qq) cut -d : -f9 ./phone.bak | grep $key_wo
8、rd | grep -f- ./phone.bak ; all) grep $key_word ./phone.bak ; esac return 0#search_menu()while :doechoecho =SearchMenu= echo 1) Search By Name echo 2) Search By Phone Numberecho 3) Search By QQ/MSN echo 4) Search In Allecho R) Retrun To Main Menuecho =echo echo read -n 1 -p Search Menu 1-4|Rr# sear
9、if $sear = r -o $sear = R then return fi echo read -p Enter your key word: word echo case $sear in 1) search_spe name $word ; 2) search_spe number $word ; 3) search_spe qq $word ; 4) search_spe all $word ; R|r) return 0 ;# *) echo Bad choice# ; esacdonedel_rec() echo read -p Enter The name you want
10、delete : dn $dn = qqq & return 0 echo sed -n /$dn:/p ./phone.bak read -n 1 -p The record you try to delete.y|n|q re echo if $re = y then sed -i /$dn:/d ./phone.bak elif $re = n then del_rec else return 0 fidel_menu()while :doechoecho =DelMenu= echo 1) Delete By Nameecho 2) Delete All Recordecho R) R
11、etrun To Main Menuecho = echo echo read -n 1 -p Del Menu 1-2|Rr# del echo case $del in 1) del_rec ; 2) read -n 1 -p Are You Sure y|n sure if $sure = y then rm -rf ./phone.bak touch ./phone.bak else del_menu fi ; R|r) return 0 ; # *) echo Bad choice esacdone mod_rec() echo read -p Enter The name you
12、try to modify : mod_name echo awk BEGIN FS=:;split(name:num:nicename:work:phone_num:local_num:birthday:e-mail:qq/msn:address:remarks,arry,:) if($1 = $mod_name) for(i=1;i=NF;i+) if (i=2) continue printf arryi: $i tttNew arryi: ; getline arry_newi- ; if(arry_newi = ) arry_newi=$i; if (i=5) print this
13、is : arry_new5; if (length(arry_new5)!=11 | arry_new5/a-z/|arry_new5/:punct:/) print The number is wrong try agin; i-; if (i=6) if (length(arry_new6)7 | arry_new6/a-z/|arry_new6/:punct:/) print The number is wrong ,try agin; i-; if (i=7) if(arry_new7!/12090-90-9-010-9-1230-9$/) print The day is wron
14、g,try agin i-; if (i=8) if(arry_new8!/.+.+.com$/) print The E-mail is wrong ,try agin i-; arry_new2=$2; print n; system(sleep 3); else if(NR=1) print Make sure the name is right. ; else print Modify complet ENDfor(j=1;j./.phonebook_awk.bak if(j=11) printf arry_newjn./.phonebook_awk.bak ./phone.bak l
15、ine_num=cut -d : -f2 ./.phonebook_awk.bak echo line_num is : $line_num sed -i /.*:$line_num:.*/d ./phone.bak cat ./.phonebook_awk.bak./phone.bak rm ./.phonebook_awk.bak -rf#bak_mail() read -p E-mail: mail_add if ( echo $mail_add|grep -E .+.+.com ) then mail -s phone.bat $mail_add /dev/null 2&1 retur
16、n 0 else echo Then E-mail is wrong,Now send the mail with default mail_address mail -s phone.bat 395423101 /dev/null 2&1 return 0 fi return 0#main ()test -f ./phone.bak | touch ./phone.bakcp ./phone.bak ./.phone.battest -s ./phone.bak | first_menucatEOF5 =MainMenu= 1) Print All Current Records 2) Pr
17、int All Current Records (formatted) 3) Print Names and Phone Numbers 4) Print Names and Phone Numbers (formatted) 5) Search for specific Record(s) 6) Add New Records 7) Delete Records 8) Modify Records 9) Backup to Mail R) Reflash Screen Q) Quit =EOF5while :do echo read -n 1 -p Main Menu 0-9|Rr|Qq#
18、menu echo case $menu in 1) print_all ; 2) print_all f ; 3) print_name ; 4) print_name f ; 5) search_menu ; 6) add_rec ; 7) del_menu ; 8) mod_rec ; 9) bak_mail ; Q|q) ex_menu ; R|r) clear main ;# *) echo Bad choice# ; esac donerm -rf ./.phone.batpasswordaaa() if ( grep -E password$ ./phonebook.sh ) then read -p Enter serail number: numb if $numb = 1234512345 then sed -i s/passwordaaa$/#&/ ./phonebook.sh return 0 else passwordaaa fi else return 0 fipasswordaaamain
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1