📄 delperson
字号:
#!/bin/sh # Name: delperson # Desc: del a person addressbook # Args: $1 -> name of person to delete # get the helper functions . ../ch21/libTYSP.sh PATH=/bin:/usr/bin # check that a name is given if [ $# -lt 1 ] ; then printUSAGE "`basename $0` name" exit 1 fi # check that the address book exists MYADDRESSBOOK="./addressbook" if [ ! -f "$MYADDRESSBOOK" ] ; then printERROR "$MYADDESSBOOK does not exists, or is not a file." exit 1 fi # initialize the variables holding the location of the temporary files TMPF1=/tmp/apupdate.$$ TMPF2=/tmp/abdelets.$$ # function to clean up temporary files doCleanUp() { rm "$TMPF1" "$TMPF1.new" "$TMPF2" 2> /dev/null ; } # function to exit if update filed Failed() { if [ "$1" -ne 0 ] ; then shift printERROR $@ doCleanUp exit 1 fi } # make a copy of the address book for updating, # proceed only if sucessful cp "$MYADDRESSBOOK" "$TMPF1" 2> /dev/null Failed $? "Could not make a backup of the address book." # get a list of all matching lines from the address book copy # continue if one or more matches were found grep "$1" "$TMPF1" > "$TMPF2" 2> /dev/null Failed $? "No matches found." # prompt the user for each entry that was found exec 5< "$TMPF2" while read LINE <&5 do # display each line formatted echo "$LINE" | awk -F: '{ printf "%-10s %s\n%-10s %s\n%-10s %s\n%-10s %s\n\n",\ "Name:",$1,"Email:",$2,"Address:",$3,"Phone:",$4 ; }' # prompt for each line, if yes try and remove the line promptYESNO "Delete this entry" "n" if [ "$YESNO" = "y" ] ; then # try to remove the line, store the updated version # in a new file grep -v "$LINE" "$TMPF1" > "$TMPF1.new" 2> /dev/null Failed $? "Unable to update the address book" # replace the old version with the updated version mv "$TMPF1.new" "$TMPF1" 2> /dev/null Failed $? "Unable to update the address book" fi done exec 5<&- # save the original version mv "$MYADDRESSBOOK" "$MYADDRESSBOOK".bak 2> /dev/null Failed $? "Unable to update the address book" # replace the original with the edited version mv "$TMPF1" "$MYADDRESSBOOK" 2> /dev/null Failed $? "Unable to update the address book" # clean up doCleanUp exit $?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -