日期:2014-05-16  浏览次数:20608 次

unix shell编写的简易电话本
创建目录shell: rolo.sh
PTH=/home/devt/owner/test1
if [ "$#" -ne 0 ]
then 
  $PTH/lu.sh "$@"
  exit
fi
validchoice=""
until [ -n "$validchoice" ]
do
   echo '
    Would you like to:
    1.  Look someone up 
    2.  Add someone to the phone book
    3.  Remove someone from the phone book
    please select one of the above (1-3): \c'
    read choice
    echo
    case "$choice"
    in
    1)echo "Enter name to look up: \c"
    read name
    $PTH/lu.sh "$name"
    validchoice="TRUE";;
    2)echo "Enter name to be added: \c"
    read name
    echo "Enter number: \c"
    read number
    $PTH/add.sh "$name" "$number"
    validchoice="TRUE";;
    3)echo "Enter name to be removed: \c"
    read name
    $PTH/rem.sh "$name";;
    *)echo "Bad choice";;
    esac
    done
    

?创建删除记录的shell:rem.sh

?在同级目录下建立temp/phonebook

if [ "$#" -ne 1 ]
then 
  echo "the wrong parameter."
  echo "usage:rem name."
  exit 1
fi
matches=$(grep "$1" phonebook | wc -l)
if [ "$matches" -gt 1 ]
then
   echo "more than one matched"
elif [ "$matches" -eq 1 ]
then
   grep -v "$1" phonebook > ./temp/phonebook
   mv ./temp/phonebook phonebook
   else
   echo "can't fine the $1"
fi

?

创建查询shell:lu.sh

grep "$1" phonebook

?创建增加记录的shell:add.sh

echo "$1 $2" >> phonebook
sort -o phonebook phonebook

?改变好文件的权限,运行./rolo.sh即可。