|
概述:实现基本的查询,新增,删除,修改以及借用,合并多人的电话本数据文件之功能!
包含:几乎所有unix shell最基本的命令(awk,sed,grep,sort..)和基本语法(函数,变量,环境..)
要点:注意程序的交互友好性和可读可维护性,以及代码的简洁合理性。
以下做为参考,限于本人水平,欢迎大家加以完善和优化
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- #!/bin/bash
- #(注意 上句 内容不可以有其他,后面不能加其他,保护用#注释内容,特别)
- #(以下命令用bash shell 来解释 :提示:shell有多种类型,如ksh,csh,rsh...)
- #declare whole variable phonebook
- phonebook=~/phonebook #(次程序用到的电话本数据文件)
- trap "" 2 #(忽略中断干扰)
- # initialize field definition
- # save field to use saparator ^ in a entry #(phonebook中字段用^分隔来存储有#如/etc/passwd)
- phbook[0]="name" #not be omitted
- phbook[1]="mail" #not be omitted
- phbook[2]="telnum"
- phbook[3]="birthday"
- phbook[4]="sex"
- phbook[5]="remark" #not more than 30 words
- if [ ! -e "$phonebook" ] #(给用户初始化一个phonebook,如果HOME下不能写,就退出)
- then
- echo "$phonebook not exist!"
- echo -e "Should I create it for you,(yes/no) \c"
- read -r answer
- if [ "$answer" != yes -o "$answer" != y ]
- then
- echo "Not Create !"
- echo "You can choise "8" to try to use other phonebook"
- exit 1
- fi
- > ~/phonebook 2>/dev/null
- if [ $? != 0 ]
- then
- echo "Create failure!"
- echo "Failure possibly is due to directory attribute"
- exit 1
- fi
- elif [ ! -f $phonebook ]
- then
- echo "$phonebook is not a common file"
- exit 1
- fi
- # format output entry, used by other function #(先定义函数,为了后面的应用)
- display () { entry="$1^" #(显示函数,格式化输出技巧)
- OIFS=$IFS #(注意数组的使用,循环结构)
- IFS=^
- set -- $entry
- IFS=$OIFS
- echo
- echo "============================================"
- typeset -i j=0
- for i
- do
- printf "| %-9.9s:%-30.30s |\n" "${phbook[j]}" "$i"
- j=$((j+1))
- done
- echo "============================================"
- echo "Press Enter to continue..."
- }
- #lu: locate entry ,it is about to used as a funtion
- lu () { grep -i "^$1\^" $phonebook >/tmp/lumatch$$
- if [ $? != 0 ]
- then
- echo "$1 not found "
- else
- lumatch="$(cat /tmp/lumatch$$)"
- display "$lumatch"
- rm -f /tmp/lumatch$$
- fi
- }
- #add: add a entry register in being used phonebook
- add () { echo "$1" >>$phonebook
- sort -uo $phonebook $phonebook
- info=$(echo "$1" |awk -F'^' '{print $1}')
- echo "$info succeeded to add"
- }
- #change: change a entry
- change () { grep -i "^$1\^" $phonebook >/tmp/chmatch$$
- if [ ! -s /tmp/chmatch$$ ]
- then
- echo "$1 not found"
- rm -f /tmp/chmatch$$
- else
- chmatch="$(cat /tmp/chmatch$$)"
- display "$chmatch"
- echo -e "change this entry register (yes/no): \c"
- read -r answer
- if [ "$answer" != y -a "$answer" != yes ]
- then
- echo -e "not to change entry"
- rm -f /tmp/chmatch$$
- else #(下面命令很重要,学会有助与shell)
- cut -d"^" -f2- /tmp/chmatch$$ >/tmp/chmatch2$$
- tr '^' '\n' < /tmp/chmatch2$$ >/tmp/chmatch$$
- vi /tmp/chmatch$$
- echo "$1" >/tmp/chmatch3$$
- cat /tmp/chmatch3$$ /tmp/chmatch$$ >/tmp/chmatch2$$
- sed -n '1,6p' /tmp/chmatch2$$ >/tmp/chmatch$$
- paste -d"^" -s /tmp/chmatch$$ >/tmp/chmatch2$$
- grep -v "^$1\^" $phonebook >/tmp/phonebook2$$
- cat /tmp/chmatch2$$ >>/tmp/phonebook2$$
- sort -o /tmp/phonebook2$$ /tmp/phonebook2$$
- cp /tmp/phonebook2$$ $phonebook
- rm -f /tmp/chmatch$$ /tmp/chmatch[2-3]$$ /tmp/phonebook2$$
- echo -e "Succeed in changing "$1" entry register"
- fi
- fi
- }
- #rem: delete a entry ,it is a funtion
- rem () { grep -i "^$1\^" $phonebook >/tmp/remmatch$$
- if [ $? != 0 ]
- then
- echo "$1 not found "
- rm -f /tmp/rematch$$
- else
- remmatch="$(cat /tmp/remmatch$$)"
- display "$remmatch"
- rm -f /tmp/remmatch$$
- echo
- echo -e "delete this entry, (yes/no) \c"
- read -r answer
- if [ "$answer" = y -o "$answer" = yes ]
- then
- sed "/^$1\^/d" $phonebook >/tmp/phonebook$$
- cp /tmp/phonebook$$ $phonebook
- rm -f /tmp/phonebook$$
- echo "$1 succeeded to be deleted"
- else
- echo -e "$1 not deleted!"
- fi
- fi
- }
- #showlist: to show all the content of being used phonebook
- showlist () { echo
- echo "================================================"
- printf "| %-30.30s %-30.30s\n" "${phbook[0] |}" "${phbook[1]}"
- awk -F'^' '{printf "| %-30.30s %-30.30s |\n" ,$1,$2}' $phonebook \
- |more
- echo "================================================"
- }
- #to combine another phonebook and join up
- joindb () { grep "^" "$1" >/tmp/joindb$$
- if [ $? != 0 ]
- then
- echo "not matched database"
- rm -f /tmp/joindb$$
- exit 1
- fi
- cat /tmp/joindb$$ >> $phonebook
- sort -uo $phonebook $phonebook
- rm -f /tmp/joindb$$
- echo "succeed in incorporating database "
- }
- #(程序初始化完成,下面就是次shell主体,注意循环的使用,和友好性)
- #(考虑到反复操作大量数据的可能,所以再操作后不退出,致使代码有重复现象)
- # initialization finished ,main shell now start implementing
- until [ "$choice" = 0 ]
- do
- echo -e "
- this is a process about telphone book operation
- would you like to:
-
- 1. Look someone up
- 2. Add someone to the phone book
- 3. Delete someone register from the phonebook
- 4. Change a entry register
- 7. Show all the phonebook content
- 8. borrow other phonebook
- 9. join another phonebook to current with ^ fields separator
- 0. exit program
- please select one of the above (1-4): \c "
- read -r choice
- echo
- case "$choice"
- in
- 1) echo -e "Enter name to look up: \c"
- read -r name
- name="$(echo "$name" |tr -d "^")"
- lu "$name"
- echo
- while true
- do
- echo -e "continue lookup,(yes/no): \c "
- read -r answer
- if [ "$answer" = n -o "$answer" = no ]
- then
- continue 2
- else
- echo -e "Enter name to look up: \c"
- read -r name #(学会过滤不必要的输入内容,注意read -r的使用)
- name="$(echo "$name" |tr -d "^")"
- lu "$name"
- fi
- done ;;
- #2 for indenting script ,temporarily name function
- 2) addtemp () {
- echo -e "Enter name: \c"
- read -r name
- while [ "$name" = "" ]
- do
- echo -e "Enter name: \c"
- read -r name
- done
- name="$(echo "$name" |tr -d "^")"
- grep -i "^$name\^" $phonebook >/dev/null
- while [ $? = 0 ]
- do
- echo
- echo -e "$name had existed,key in other name"
- continue 2
- done
- echo -e "Enter mailbox: \c"
- read -r mailbox
- echo "$mailbox" |grep "@" >/dev/null
- mailflag=$?
- while [ "$mailbox" = "" -o "$mailflag" != 0 ]
- do
- echo -e "Enter mailbox and include '@' char : \c"
- read -r mailbox
- echo "$mailbox" |grep "@" >/dev/null
- mailflag=$?
- done
- mailbox="$(echo "$mailbox" |tr -d "^")"
- echo -e "Enter phone number: \c"
- read -r telnum
- telnum="$(echo "$telnum" |tr -d "^")"
- echo -e "Enter birthday: \c"
- read -r birthday
- birthday="$(echo "$birthday" |tr -d "^")"
- echo -e "Enter sex: \c"
- read -r sex
- sex="$(echo "$sex" |tr -d "^")"
- echo -e "Enter remark: \c"
- read -r remark
- remark="$(echo "$remark" |tr -d "^")"
- }
- addtemp
- add "$name^$mailbox^$telnum^$birthday^$sex^$remark"
- echo
- while true
- do
- echo -e "continue add,(yes/no): \c "
- read -r answer
- if [ "$answer" = n -o "$answer" = no ]
- then
- continue 2
- else
- addtemp
- add "$name^$mailbox^$telnum^$birthday^$sex^$remark"
- echo
- fi
- done ;;
- #3 for indenting script, temporarily name funtion
- 3) deltemp () { echo -e "Enter name to be deleted: \c"
- read -r name
- while [ "$name" = "" ]
- do
- echo -e "Enter name to be deleted: \c"
- read -r name
- done
- name="$(echo "$name" |tr -d "^")"
- }
- deltemp
- rem "$name"
- echo
- while true
- do
- echo -e "continue rem,(yes/no): \c "
- read -r answer
- if [ "$answer" = n -o "$answer" = no ]
- then
- continue 2
- else
- deltemp
- rem "$name"
- echo
- fi
- done ;;
- 4 ) changetemp () { echo -e "Enter name to be changed: \c"
- read -r name
- while [ "$name" = "" ]
- do
- echo -e "Enter name to be changed: \c"
- read -r name
- done
- name="$(echo "$name" |tr -d "^")"
- }
- changetemp
- change "$name"
- echo
- while true
- do
- echo -e "continue change,(yes/no): \c "
- read -r answer
- if [ "$answer" = n -o "$answer" = no ]
- then
- continue 2
- else
- changetemp
- change "$name"
- echo
- fi
- done ;;
- 7 ) echo " **********************phonebook content***********************"
- showlist
- echo " **************************************************************"
- ;;
- 8) echo -e "Enter a username with holding phonebook: \c"
- read -r name
- eval phonebook2=~$name/phonebook
- while [ ! -f "$phonebook2" ]
- do
- echo -e "$phonebook2 not exist;continue try (yes/no): \c"
- read -r answer
- if [ "$answer" != y -a "$answer" != yes ]
- then
- continue 2
- fi
- echo -e "Enter a username with holding phonebook: \c"
- read -r name
- eval phonebook2=~$name/phonebook
- done
- if [ $phonebook = $phonebook2 ]
- then
- echo
- echo "operation succeed ,then user is yourself"
- else
- echo
- echo "succeed in borrowing $name's phonebook"
- fi
- phonebook=$phonebook2
- echo
- ;;
- #9 for indenting script ,temporarily name fucntion
- 9) jointemp () { echo -e "Enter a username with holding phonebook: \c"
- read -r name
- eval phonebook2=~$name/phonebook
- until [ -f "$phonebook2" -a -s "$phonebook2" -a "$phonebook2" \
- != "$phonebook" ]
- do
- echo -e "$phonebook2 not exits or current;continue try (yes/no): \c"
- read -r answer
- if [ "$answer" != y -a "$answer" != yes ]
- then
- continue 2
- fi
- echo -e "Enter a username with holding phonebook: \c"
- read -r name
- eval phonebook2=~$name/phonebook
- done
- }
- jointemp
- joindb "$phonebook2"
- echo
- while true
- do
- echo -e "continue join database (yes/no): \c "
- read -r answer
- if [ "$answer" != yes -a "$answer" = y ]
- then
- continue 2
- else
- jointemp
- joindb "$phonebook2"
- echo
- fi
- done
- ;;
- 0) exit 1 ;;
- *) echo "Bad choice" ;;
- esac
- done
复制代码 |
|