linux脚本课程设计通讯录,shell编写的一个通讯录脚本

#!/dev/bash

# Name of address book

BOOK="address-book.txt"

exit=0

add() {

# Ask the user for a name and assign to a variable

echo -n "Name of person: "

read name

# Ask the user for a phone number and assign to a variable

echo -n "Phone number: "

read phone

# Echo the answers and ask for confirmation

echo "Should I enter the values:"

echo -e "$name ; $phone \n"

echo -n "y/n: "

read answer

if [ "$answer" == "y" ]

then

# Write the values to the address book

echo "$name ; $phone" >>$BOOK

else

# Give the user a message

echo "$name ; $phone NOT written to $BOOK"

fi

}

list() {

# Print the book with line numbers and paused with less

nl --number-separator=":    " $BOOK | less

}

find() {

# Ask the user what to look for.

echo -n "What person or number are you seeking: "

read find

# Print the header before the answer

echo "Name ; Phone number"

grep -i $find $BOOK

}

del() {

# Ask the user which line to delete

echo -n "Which line should I delete: "

read number

# Rename the file before deleting

mv $BOOK boo.txt

# Add line numbers and delete against that number

nl --number-separator=":" boo.txt | grep -v $number: | awk -F: '{print $2}' |  tee $BOOK

}

main() {

while [ $exit -ne 1 ]

do

echo "What operation do you want?"

echo -e "add, list, find, del, exit: "

read answer

if [ "$answer" = "add" ]

then

add

elif [ "$answer" = "list" ]

then

list

elif [ "$answer" = "find" ]

then

find

elif [ "$answer" = "del" ]

then

del

elif [ "$answer" = "exit" ]

then

exit=1

else

echo "I do not understand the command."

fi

done

exit 0

}

main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值