|

楼主 |
发表于 2005-3-22 19:14:26
|
显示全部楼层
原文件:不过不含中文注释的 ,你首先需要给这个shell可执行权限,而且你登陆的HOME目录下可建立新文件才能,不然这个shell会给你提示,。
下面有个“笑脸符号,没办法,总是有”
笑脸处代码为:
printf "| %-9.9s-30.30s |\n" "${phbook[j]}" "$i"
笑脸为 : 和 % 如(AB) ,不能写倒一起去,一些就变了笑脸。哈哈,下面为代码
#!/bin/bash
#declare whole variable phonebook
phonebook=~/phonebook
trap "" 2
# initialize field definition
# save field to use saparator ^ in a entry
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 40 words
if [ ! -e "$phonebook" ]
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 " ress 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
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 "
}
# 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
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 |
|