LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 850|回复: 3

谁能解析一下getopts 这个命令

[复制链接]
发表于 2003-5-18 23:44:09 | 显示全部楼层 |阅读模式
谁能解析一下getopts 这个命令
发表于 2003-5-19 01:36:21 | 显示全部楼层
getopts用于处理命令行选项的操作.以下是:
http://www.linuxsir.cn/bbs/showthread.php?threadid=28945
置顶的帖子:<BASH编程入门>,请仔细看看;)
使用getopts命令读取unix格式选项
    UNIX格式选项指如下格式的命令行参数:
    command -options parameters
  
    使用格式:
    getopts option_string variable
  
    具体使用方法请参考getopts的在线文档(man getopts).
  
    示例如下:
  
         #newdate
         if [ $# -lt 1 ]
         then
             date
         else
            while getopts mdyDHMSTjJwahr OPTION
            do
               case $OPTION
               in
                  m) date '+%m ';;  # Month of Year
                  d) date '+%d ';;  # Day of Month
                  y) date '+%y ';;  # Year
                  D) date '+%D ';;  # MM/DD/YY
                  H) date '+%H ';;  # Hour
                  M) date '+%M ';;  # Minute
                  S) date '+%S ';;  # Second
                  T) date '+%T ';;  # HH:MM:SS
                  j) date '+%j ';;  # day of year
                  J) date '+%y%j ';;# 5 digit Julian date
                  w) date '+%w ';;  # Day of the Week
                  a) date '+%a ';;  # Day abbreviation
                  h) date '+%h ';;  # Month abbreviation
                  r) date '+%r ';;  # AM-PM time
                  \?) echo "Invalid option $OPTION";;
               esac
            done
         fi
  
         $ newdate -J
         94031
         $ newdate -a -h -d
         Mon
         Jan
         31
         $ newdate -ahd
         Mon
         Jan
         31
         $
  
  
         示例程序:复制程序
  
         # Syntax: duplicate [-c integer] [-v] filename
         #    where integer is the number of duplicate copies
         #    and -v is the verbose option
  
         COPIES=1
         VERBOSE=N
  
  
         while getopts vc: OPTION
         do
            case $OPTION
            in
               c) COPIES=$OPTARG;;
               v) VERBOSE=Y;;
               \?) echo "Illegal Option"
                   exit 1;;
            esac
         done
  
         if [ $OPTIND -gt $# ]
         then
            echo "No file name specified"
            exit 2
         fi
  
  
         shift `expr $OPTIND -1`
  
         FILE=$1
         COPY=0
  
         while [ $COPIES -gt $COPY ]
         do
            COPY=`expr $COPY + 1`
            cp $FILE ${FILE}${COPY}
            if [ VERBOSE = Y ]
            then
               echo ${FILE}${COPY}
            fi
         done
  
  
         $ duplicate -v fileA
         fileA1
         $ duplicate -c 3 -v fileB
         fileB1
         fileB2
         fileB3
  
发表于 2003-5-19 01:38:52 | 显示全部楼层
提问之前,先到置顶区或者精华区看一看,那里有很多兄弟们提供的资料,
发表于 2003-5-19 11:48:22 | 显示全部楼层
没弄懂OPTIND这个变量的具体含意。书上也没有查到

另外这个脚本两个小问题:

  1.          # Syntax: duplicate [-c integer] [-v] filename
  2.          #    where integer is the number of duplicate copies
  3.          #    and -v is the verbose option
  4.   
  5.          COPIES=1
  6.          VERBOSE=N
  7.   
  8.   
  9.          while getopts vc: OPTION
  10.          do
  11.             case $OPTION
  12.             in
  13.                c) COPIES=$OPTARG;;
  14.                v) VERBOSE=Y;;
  15.                \?) echo "Illegal Option"
  16.                    exit 1;;
  17.             esac
  18.          done
  19.          if [ $OPTIND -gt $# ]
  20.          then
  21.             echo "No file name specified"
  22.             exit 2
  23.          fi
  24.   
  25.   
  26.          shift `expr $OPTIND - 1` [color=red]#a space before "1" is missing[/color]
  27.   
  28.          FILE=$1
  29.          COPY=0
  30.   
  31.          while [ $COPIES -gt $COPY ]
  32.          do
  33.             COPY=`expr $COPY + 1`
  34.             cp $FILE ${FILE}${COPY}
  35.             if [ $VERBOSE = Y ] [color=red]#"$" is missing[/color]
  36.            then
  37.               echo ${FILE}${COPY}
  38.            fi
  39.           done
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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