Shell 电话本
之前用C写了个电话本,今天上班无聊将此电话本改用shell编写。
#!/bin/sh#Function:index\add\del;Save in file phoneno.txt#Auther:axengine#DATE:201008#BLOG:WWW.VEIDOO.NET################index content################function index { echo "Please input the name:" read name grep ${name} ./phoneno.txt name=""}################add function#################function add { echo "增加信息,请输入姓名:" read name while [ -z $name ] do echo "姓名不能为空,请重新输入姓名:" read name done echo "增加信息,请输入性别:" read sex echo "增加信息,请输入年龄:" read age echo "增加信息,请输入电话号码:" read tellno while [ -z $tellno ] do echo "电话号码不能为空,请重新输入电话号码:" read tellno done echo "增加信息,请输入QQ:" read QQ echo "增加信息,请输入E-MAIL:" read email echo "增加信息,请输入出生年月:" read birthday echo $name \| $sex \| $age \| $tellno \| $QQ \| $email \| $birthday \| >> phoneno.txt if [ $? -ne 0 ] then echo "Add error\n" fi echo "Add OK!"}#######Function modify##################Works:Delete the old and insert The Newfunction modify { cat phoneno.txt | more echo "Please chose which your want to modify?Input the name:" read name echo "You will modify this content:" grep ${name} ./phoneno.txt sed -i -e '/'"$name"'/d' ./phoneno.txt name="" echo "Input the New content!" echo "增加信息,请输入姓名:" read name while [ -z $name ] do echo "姓名不能为空,请重新输入姓名:" read name done echo "增加信息,请输入性别:" read sex echo "增加信息,请输入年龄:" read age echo "增加信息,请输入电话号码:" read tellno while [ -z $tellno ] do echo "电话号码不能为空,请重新输入电话号码:" read tellno done echo "增加信息,请输入QQ:" read QQ echo "增加信息,请输入E-MAIL:" read email echo "增加信息,请输入出生年月:" read birthday echo ${name}|${sex}|${age}|${tellno}|${QQ}|${email}|${birthday}| >> phoneno.txt if [ $? -ne 0 ] then echo "Modify error\n" fi echo "Modify OK!"}############Delete#######################function delete { echo "Which line you want to del?Please input the name:" read delname delline=`grep $delname ./phoneno.txt` sed -i -e '/'"$name"'/d' ./phoneno.txt if [ $? -ne 0 ] then echo "Del error;" fi}##############menu function###################menu() {echo "1:查询;"echo "2:增加;"echo "3:修改;"echo "4:删除;"echo "0:退出;"echo -n "Input your choice:"read chosecase $chose in1) index;;2) add;;3) modify;;4) delete;;0) exit;;*) default menu;;esac}########Main{}#####################if [ -d ./phoneno.txt ]then echo "File phoneno.txt doesn\'t exist" touch phoneno.txt fimenu
#!/bin/shif [ $1 = 'r' ]then name=$2 awk 'BEGIN{FS=":::"}{if($1 ~ /'$name'/)print $1, $2;}' <phoneno.datelif [ $1 = 'w' ]then name=$2 fs=":::" number=$3 echo $2 $fs $number >>phoneno.datelse echo "Useage:phoneno.sh w Name Number\n phoneno.sh r Name"fi
[解决办法]
if [ $1 = 'r' ]; then awk 'BEGIN{FS=":::"}{if($1 ~ /'$2'/)print $1, $2;}' <phoneno.dat; else echo $2 ":::" $3 >>phoneno.dat;fi
[解决办法]
echo "增加信息,请输入性别:"
read sex
echo "增加信息,请输入年龄:"
read age
echo "增加信息,请输入电话号码:"
read tellno
while [ -z $tellno ]
do
echo "电话号码不能为空,请重新输入电话号码:"
read tellno
done
echo "增加信息,请输入QQ:"
read QQ
echo "增加信息,请输入E-MAIL:"
read email
echo "增加信息,请输入出生年月:"
read birthday
echo $name \| $sex \| $age \| $tellno \| $QQ \| $email \| $birthday \| >> phoneno.txt
不知这段代码是否可以写在一个方法里面,方便复用。
开始学了两天的bash shell了,代码基本上看得懂,不过暂时还写不出来。对于junior,是学习shell很不错的范例代码