|
|
作者:chauney 出处:Unix爱好者家园unix-cd.com
|
#!/bin/sh
source add
source show
source delete
function menu()
{
echo -e "\33[34m"
cat << EOF
What you want to do? Please input you choice:
[1] Add a person into the address book!
[2] Check out a person's data in the address book!
[3] Delete a person from the origin address book!
[4] Quit without do anything!
EOF
echo -ne "Please select [ 1, 2, 3, or 4 ]:\33[0m"
while read choice
do
case $choice in
1 ) add;;
2 ) show ;
3 ) delete ;
4 ) echo ByeBye!; exit 0 ;
* ) echo -ne "\33[31mError! Please input [ 1, 2, 3, or 4 ]: \33[0m";;
esac
done
}
echo -e "\33[32m\t\tWellcome to My addressbook!\33[0m"
menu
function checkperson ()
{
if [ -z "$name" ]; then
echo -e "\33[31mError! the Name is incorrect!\n Please Input the person's name and other messages again: \33[0m"
addperson
fi
if [ -f "addressbook" ]
then
showname
if [ ! -z "$showname" ] then
echo "The name you input has already exist! Do you want to delete the origin name? [ y or n or q ( quit to main menu )]"
while read
do
case $REPLY in
y|Y|[Yy][Ee][Ss] )
awk -F: -v new=$name '{ if ( $1 != new ) print $0; }' addressbook > /tmp/tmp$$
mv -f /tmp/tmp$$ addressbook 2> /dev/null
check
identify
;
n|N|[Nn][Oo] )
echo Please input the name again:
read name
checkperson
check
identify
;
q|Q )
menu
;
* )
echo "Error! Please input y or n or q (quit to main menu )!"
;
esac
done
fi
fi
}
function checkmail ()
{
if [ -z $mail ]; then mail="[email protected]"; fi
mail_check=`echo $mail | sed -n '/\@*\./p'`
if [ -z $mail_check ]
then
echo -ne "The E-mail you input is wrong!\nPlease input again: "
read mail
checkmail
fi
}
function checkphone ()
{
if [ -z "$phone" ]; then phone=0; fi
if [ -z "$mobile" ]; then mobile=0; fi
phone_check=`echo $phone | tr ':;",.+<>?/~\!=@#$%^&*[a-zA-Z]{}|' ' ' `
mobile_check=`echo $mobile | tr ':;",.+<>?/~\!=@#$%^&*[a-zA-Z]{}|' ' ' `
if [ "$phone_check" != "$phone" ]
then
echo -ne "The Phone Number you input is incorrect!\nPlease input again: "
read phone
checkphone
elif [ "$mobile_check" != "$mobile" ]
then
echo -ne "The Mobile Phone Number you input is incorrect!\nPlease input again: "
read mobile
checkphone
fi
}
function redo ()
{
select pick in "Name" "E-mail" "Address" "Phone Number" "Moblie Phone Number" "Other Messages" "Exit to the identify menu" "Exit to the main menu" "Quit without save"
do
case $pick in
Name )
echo Please input the name again:
read name
checkperson
redo
;
E-mail )
echo Please input the E-mail again:
read mail
checkmail
redo
;
Address )
echo Please input the address again:
read address
redo
;
"Phone Number" )
echo Please input the phone number again:
read phone
checkphone
redo
;
"Moblie Phone Number" )
echo Please input the mobile phone number again:
read mobile
checkphone
redo
;
"Other Messages" )
echo Please input the other messages again:
read message
redo
;
"Exit to the main menu" )
menu
;
"Exit to the identify menu" )
identify
;
"Quit without save" )
exit 0
;
* ) echo -e "This is not a valid choice. Try again:"
pick=
;
esac
done
}
function check ()
{
checkperson
checkmail
checkphone
if [ -z "$address" ]; then address="Null"; fi
if [ -z "$message" ]; then message="Null"; fi
}
function identify ()
{
cat << DATA
The data you input is on the next:
Name: $name
E-mail: $mail
Address: $address
Phone Number: $phone
Mobile Phone Number: $mobile
Other Message: $message
Is it right?[ y or n ]:
DATA
while read answer
do
case $answer in
y|Y|[Yy][Ee][Ss] )
echo $name:$mail:$address:$phone:$mobile:$message >> addressbook
echo Do you want to add another person [ y or n ,and any other key will exit! ]:
read
case $REPLY in
y|Y|[Yy][Ee][Ss] ) add;;
n|N|[Nn][Oo] ) menu;;
* ) exit 0;;
esac
;
n|N|[Nn][Oo] ) echo Which one is incorrect? Pick out it :; redo;;
* ) echo -ne "\33[31mError! Please input [ y or n ]: \33[0m"
esac
done
}
function addperson ()
{
echo -n "Input the Name: "; read name
echo -n "Input the E-mail: "; read mail
echo -n "Input the Address: "; read address
echo -n "Input the Phone Number: "; read phone
echo -n "Input the Mobile Phone Number: "; read mobile
echo -n "Input the other messages if necessary: "; read message
check;
identify;
}
function add ()
{
echo Please follow the next steps !
stty erase '^?'
addperson
}
function answer ()
{
read
case $REPLY in
y|Y|[Yy][Ee][Ss] ) show ;
n|N|[Nn][Oo] ) menu ;
* ) exit 0;;
esac
}
function showname ()
{
showname=`awk -F: -v new=$name '{ if ( $1 == new ) print $0; }' addressbook | awk -F: '{ printf "%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n\n", "Name: ", $1, "E-mail: ",$2, "Address: ", $3, "Phone Number: ", $4, "Moblie Phone Number: ", $5, "Other Messages: ", $6 }' `
}
function showall ()
{
awk -F: '{ printf "%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n%-10s%s\n\n", "Name: ", $1, "E-mail: ",$2, "Address: ", $3, "Phone Number: ", $4, "Moblie Phone Number: ", $5, "Other Messages: ", $6 }' addressbook
}
function show ()
{
echo -ne "\33[33mWhose data you want to know ? \n\33[34mType his or her name directly ( Type "all" for check out for all people! ): \33[0m"
read name
if [ -f "addressbook" ]
then
if [ $name == "all" ]
then
showall
echo -e "\33[33mType "y" to check a person's data, and "n" to the main menu, other keys will exit!\33[0m"
answer
else
showname
if [ -z "$showname" ]; then
echo -e "The person you want to know does not exist! \nDo you want to try again or check out other's data? [y or n ,and any other key will exit! ]"
answer
else
echo -e "$showname"
echo Do you want to check another person\'s data? [y or n ,and any other key will exit! ]
answer
fi
fi
else echo -e "\33[31mThe addressbook isn't exist!\33[0m"; menu
fi
}
function deldata ()
{
showname
if [ -z "$showname" ]; then
echo -e "\33[31mThe person you want to delete does not exist! Please check the name first!:\33[0m"
menu
else
echo -e "The person 's data is :\n$showname"
echo -ne "\33[34m\tIs it right? \33[0m[ y or n ]: "
read
if [[ "$REPLY" == [Yy] ]]; then
awk -F: -v new=$name '{ if ( $1 != new ) print $0; }' addressbook > /tmp/tmp$$
mv -f /tmp/tmp$$ addressbook 2> /dev/null
if [ $? == 0 ]; then
echo The data is updated successfully!
menu
else
echo There are something unexpected happen! Please try again!
delete
fi
else
echo -e "\33[31m\t\tSkip to the main menu\33[0m"
menu
fi
fi
}
function delete ()
{
echo -n "Type a name you want to delete: "
read name
deldata
}
|
|
|
|
|