shell通讯录(3)

#!/bin/bash 
# delperson
# del a person from addressbook
# args:$1-> name of person to delete


#get the helper function
. /home/addressbook/libComm.sh
PATH=/bin:/usr/bin


if [ $# -lt 1 ];then
    printUSAGE "`basename $0 ` name "
    exit 1
fi


MYADDRESSBOOK="/home/addressbook/addressbook"
if [ ! -f "$MYADDRESSBOOK" ];then
    printERROR "$MYADDRESSBOOK does not exists, or is not a file"
    exit 1
fi


#temp file location
TMPF1=/home/addressbook/apupdate.$$
TMPF2=/home/addressbook/abdelets.$$
touch $TMPF1 $TMPF2;


doCleanUp(){
    rm "$TMPF1" "$TMPF1.new" "$TMPF2" 2>/dev/null
}
Failed() {
    if [ "$1" -ne 0 ];then
        shift
        printERROR $@
        doCleanUp
        exit 1
    fi
}




cp "$MYADDRESSBOOK" "$TMPF1";
# 2>/dev/null
Failed $? "could not make a backup of the address book"


grep "$1" "$TMPF1" > "$TMPF2";
# 2>/dev/null
Failed $? " no matches found"


# prompt the user for each entry that was found
#exec 5<"$TMPF2"
for  LINE in `cat $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",
         "Name:",$1,"Email:",$2,"Address:",$3,"Phone:",$4;


    }'
    #prompt to delete
    promptYESNO "delete this entry" "n"
    echo 'yesno=' $YESNO
    if [ "$YESNO" = "y" ];then
        grep -v "$LINE" "$TMPF1" > "$TMPF1.new" 2>/dev/null
        Failed $? "unable to update the address book"
        # replace the old file
        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 $?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值