日期:2014-05-16 浏览次数:20949 次
#!/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 New function 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 chose case $chose in 1) 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 fi menu
#!/bin/sh if [ $1 = 'r' ] then name=$2 awk 'BEGIN{FS=":::"}{if($1 ~ /'$name'/)print $1, $2;}' <phoneno.dat elif [ $1 = 'w' ] then name=$2 fs=":::" number=$3 echo $2 $fs $number >>phoneno.dat else 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