LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: angelsss

一个学unix shell 都要考试的电话本程序

[复制链接]
发表于 2005-3-22 10:24:26 | 显示全部楼层

什么意思

什么意思
回复 支持 反对

使用道具 举报

 楼主| 发表于 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
回复 支持 反对

使用道具 举报

发表于 2005-3-23 21:02:59 | 显示全部楼层
终于解决了
原因是直接复制会加近一些非法字符
如果用tail命令把它导入另外一个文件就能看到每一行的结尾加了一个^M
用cat等命令不行
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-24 20:23:43 | 显示全部楼层
那你能运行了吗?
回复 支持 反对

使用道具 举报

发表于 2005-3-25 14:06:25 | 显示全部楼层
是的,但是我是把所有注释都去掉了。还没有作全面的功能测试
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-25 22:08:39 | 显示全部楼层
最后8. borrow other phonebook
9. join another phonebook to current with ^ fields separator
...................................

相信你能看懂 ..  8: 如果自己没有电话本..就 强一个.哈哈
                9 : 自己强的不够,还要私吞人家的. 哈哈.
回复 支持 反对

使用道具 举报

发表于 2005-3-28 17:22:44 | 显示全部楼层
好贴!!
回复 支持 反对

使用道具 举报

发表于 2005-3-31 10:50:20 | 显示全部楼层

为什么不用getopts把命令参数加进去

学习中问一句
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-31 17:52:46 | 显示全部楼层
getopts 用在小shell中, 基本不用在有目录形式的中大型shell 中,比如squirrel 松鼠的 conf.pl 也是不带参数
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-31 18:02:23 | 显示全部楼层
一个cp的 own版本  :mycp ---getopts 的简单应用例子

#!/bin/bash
# copy a file --last version
filelist=
copylist=
coveflag=false
#process the arguments,storing all but the last in filelis
while getopts n option
do
  case "$option"
  in
    n) coveflag=true
       shift ;;
   \?) echo "Usage: ${0##*/} -n file file  "
       echo "       ${0##*/} -n file(s) dir"
       echo " -n mean to force covering  ;;
  esac
done
numargs=$#
while [ "$#" -gt 1 ]
do
   filelist="$filelist $1"
   shift
done
to="$1"
if [ "$numargs" -lt 2 -o "$numargs" -gt 2 -a ! -d "$to" ]
then
   echo "usage: ${0##*/} file1 file2"
   echo "  ${0##*/} file(s) dir"
exit 1
fi
#sequence through each file in filelist
for from in $filelist
do
  # see if destination file is a directory
if [ -d "$to" ]
then
    tofile="$to/$(basename $from)"
else
    tofile="$to"
fi
# if user says its okay to overwrite
if [ -e "$tofile"  ]
then
    if [ $coveflag = true ]
    then
       copylist="$copylist $from"
    else
       echo -e "$tofile already exists; overwrite (yes/no)? \c"
       read answer
       if [ "$answer" = yes -o "$answer" = y ]
       then  
         copylist="$copylist $from"
       fi
    fi
else
     copylist="$copylist $from"
fi
done
# now do the copy - first make sure there's something to copy
if [ -n "$copylist" ]
then
   cp $copylist $to   
fi
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表