shell电话本

前几天在MSDN上看到有人用shell写了个电话本程序,为了巩固一下以前学过的, 自己也来弄一个shell电话本[绝非抄袭]。其实不能算一个电话本,因为只有两个域:名字和地址,权当练习,不尽完美之处,请海涵:D

#!/bin/sh
#Date: 2010-08-22
#QQ: 32281309
#MSN: pgj1207@hotmail.com
#Site: www.genepeng.com

function AddUser()
{
while :
do
echo -n “Enter user name: ”
read UserName
if [ “$UserName” = “” ]; then
continue
else
break
fi
done

while :
do
echo -n “Enter Address: ”
read Address
if [ “$Address” = “” ]; then
continue
else
break
fi
done

echo -e “$UserName\t$Address” >> $FileName
}

function QueryUser()
{
echo “Enter user name to query”

while :
do
echo -n “Enter user name: ”
read UserName
if [ “$UserName” = “” ]; then
continue
else
break
fi
done

User=`grep “$UserName\>” < $FileName` if [ "$User" = "" ]; then echo "$UserName not exist" else echo "$User" fi } function DeleteUser() { echo "Enter user name to delete" while : do echo -n "Enter user name: " read UserName if [ "$UserName" = "" ]; then continue else break fi done echo "Do you really want to delete $UserName? Y/N" read YN if [ "$YN" = "y" -o "$YN" = "Y" ]; then sed "/$UserName/d" $FileName > “$FileName.tmp”
mv “$FileName.tmp” “$FileName”
fi
}

function EditUser()
{
echo “Enter user name to edit”

while :
do
echo -n “user name: ”
read UserName
if [ “$UserName” = “” ]; then
continue
else
break
fi
done

if IsUserExist $UserName; then
echo “$UserName not exist”
else
sed “/$UserName/d” $FileName > “$FileName.tmp”
mv “$FileName.tmp” “$FileName”

while :
do
echo -n “Enter Address: ”
read Address
if [ “$Address” = “” ]; then
continue
else
echo -e “$UserName\t$Address” >> $FileName
break
fi
done
fi
}

function ListAllUser()
{
cat $FileName
}

function IsUserExist()
{
User=`grep “$1\>” < $FileName` if [ "$User" = "" ]; then return 0 else return 1 fi } function ShowMenu() { echo "****** Employee Directory ****** " echo " A : AddUser " echo " Q : QueryUser " echo " D : DeleteUser " echo " E : EditUser " echo " L : ListAllUser " echo " X : Exit " echo "******************************** " } #Site: www.genepeng.com FileName="user_directory.txt" if [ ! -f $FileName ]; then > $FileName
fi

while :
do
ShowMenu
read c
case $c in
A|a) AddUser ;;
Q|q) QueryUser ;;
D|d) DeleteUser ;;
E|e) EditUser ;;
L|l) ListAllUser ;;
X|x) break ;;
*) echo “Error: Wrong choice”
continue ;;
esac
done

1 thought on “shell电话本”

  1. I have read a few good stuff here. Certainly worth bookmarking for revisiting. I wonder how much effort you put to create such a magnificent informative website.

Comments are closed.