求助:bash中如何提取命令行选项的辅助参数
Tofloor
poster avatar
careone1949
deepin
2013-10-05 03:36
Author
求助:bash中如何提取命令行选项的辅助参数

主程序名称: emacs-locale

程序目标:
* 判断并提取 -gl, -l -v 这三个选项的辅助参数,并保存到三个对应的数组中。
  数组定义为 ARRAY_GL, ARRAY_L, ARRAY_V       

示例如下:
  1. ./emacs-locale -gl zh_CN -v 24.3
Copy the Code
  1. ./emacs-locale -l zh_CN -v 24.3
Copy the Code

        说明:
        -gl 代表全局语言相关选项(Global Language);
        -l 代表当前用户语言相关选项(Language);
        -v 代表 Emacs 版本号 (Version);

* 容错功能

  ** 每个选项后允许输入多个参数,如:

  1. ./emacs-locale -gl zh_CN -v 24.3 23.1
Copy the Code
  1. ./emacs-locale -gl zh_CN zh_TW -v 24.3 23.1
Copy the Code

  ** 如果同一选项提供多次输入,则合并有效参数,如:
  1. ./emacs-locale -gl zh_CN -v 24.3 23.1 -gl zh_TW
Copy the Code
        等效于:
  1. ./emacs-locale -gl zh_CN zh_TW -v 24.3 23.1
Copy the Code


  ** 能自动过滤无辅助参数的无效选项,如
  1. ./emacs-locale -gl -gl zh_CN -v 24.3 23.1 -gl zh_TW
Copy the Code
        上面的 -gl 选项出现多次,需要过滤

* 说明
        完整的程序还有更多功能,这里仅列待解决部分的代码
相关代码见2楼

调试时发现有时能正确提取参数,但有时出现异常.
运行环境:
Ubuntu 10.04.3 i386
GNU bash, version 4.1.5(1)-release

示例代码如下:
命令行
  1. ./emacs-locale -gl gl1 gl2 gl3 -v v10 v11 -l l1 l2 -l l2 -l l3  -gl gl2 -v v21 gl
Copy the Code

输出如下(输出结果有误,-l 选项的参数未能正确提取)
----
ARRAY_GL = 'gl1 gl2 gl3'
ARRAY_L = ''
ARRAY_V = 'v10 v11'

有劳哪位高手指点一下,多谢!
Reply Favorite View the author
All Replies
careone1949
deepin
2013-10-05 03:38
#1
接 1 楼
程序代码
  1. #! /bin/bash
  2. ## coding: utf-8
  3. ## translate Emacs/XEmacs menu to other languages
  4. ## updated by Careone , 2013-10-04
  5. VERSION='2.12'
  6. # Usage: usage
  7. # Print the usage.
  8. usage () {
  9.     cat <Usage: $0 [OPTION]
  10. toggle $NAMEU menu to special language, for current user or global.
  11.   You also can translate $NAMEU menu to special language(s) if translate
  12. scripts existed. Please run '$0 -d' for translate/develop options.
  13.   -V, --version    print the version information and exit
  14.   -h, --help       print this message and exit
  15. Translate and develop related options
  16. usage:    $0 -l LANGUAGE -v VERSION
  17. examples: $0 -l zh_CN -v 24.1
  18.           $0 -l zh_CN -v 24.1 24.2
  19.           $0 -l zh_CN zh_TW -v 24.1 24.2
  20.       -gl [LANGUAGE_1 LANGUAGE_2 ...]    (root please!)
  21.         with -v, (Global) translate $NAMEU lisp/*/*.el files to special
  22.         version(s). If no VERSION given, try probe current $NAMEU version
  23.         installed instead.
  24.       -l [LANGUAGE_1 LANGUAGE_2 ...]
  25.         with -v, translate $NAMEU lisp/*/*.el files to special language(s)
  26.       -v [VERSION1 VERSION2 ...]
  27.         with -gl or -l, translate $NAMEU lisp/*/*.el files to special version(s). If
  28.         no VERSION given, try probe current $NAMEU version installed instead.
  29.       !!! NOTICE !!!
  30.        * option '-l':
  31.          translated .el files save to ~/$TRANSOF/usr/share/$NAMEL/... and
  32.          apply to current user only
  33.        * option '-gl': (root please!)
  34.          translated .el files save to /usr/share/$NAMEL/... and apply to ALL
  35.          USERS. If you just want to test your translations, please use option
  36.          '-l' instead!
  37.        
  38. EOF
  39. }
  40. ###
  41. # tag 2000
  42. ## PART 2: main
  43. # Check the arguments.
  44. for option in "$@"; do
  45.     case "$option" in
  46.     -h | --help)
  47.         usage
  48.         exit 0 ;;
  49. ## todo -------------------------
  50.     -gl | -l | -v)
  51.     declare -a ARRAY_GL
  52.     declare -a ARRAY_L
  53.     declare -a ARRAY_V
  54.     declare -a ARG
  55. ## 2013-09-20
  56. #ARGS="$@"
  57.   case "$1" in
  58.   -gl)
  59.             shift
  60.         unset ARG[@] #init array to null
  61.         ARG=( `echo -n "$@" | sed "s/ -/\n -/" | head -1` )
  62.   if [ "${#ARG[@]}" -ge 1 ];then
  63.         # if argument like "-gl -gl zh_CN", throw the first -gl
  64.         case "${ARG[0]}" in
  65.         --* | -*): ;;
  66.         *)
  67.           if [ "${#ARRAY_GL[@]}" -ge 1 ];then
  68.             ARRAY_GL=( "${ARRAY_GL[@]}" "${ARG[@]}" )
  69.           else ARRAY_GL=( "${ARG[@]}" )
  70.           fi
  71.           shift "${#ARG[@]}"       
  72. #        echo "ARRAY_GL = '${ARRAY_GL[@]}'"
  73.         esac
  74.   fi  
  75.         continue
  76.         ;;
  77.   -l)
  78.             shift
  79.         unset ARG[@] #init array to null
  80.         ARG=`echo -n "$@" | sed "s/ -/\n -/" | head -1`
  81.   if [ "${#ARG[@]}" -ge 1 ];then
  82.         # if argument like "-l -l zh_CN", throw the first -l
  83.         case "${ARG[0]}" in
  84.         --* | -*): ;;
  85.         *)
  86.         if [ "${#ARG[@]}" -ge 1 ];then
  87.             if [ "${#ARRAY_L[@]}" -ge 1 ];then
  88.             ARRAY_L=( "${ARRAY_L[@]}" "${ARG[@]}" )
  89.           else ARRAY_L=( "${ARG[@]}" )
  90.           fi
  91.           shift "${#ARG[@]}"       
  92.         fi
  93. #        echo "ARRAY_L = '${ARRAY_L[@]}'"
  94.         esac
  95.   fi  
  96.         continue
  97.         ;;
  98.   -v)
  99.             shift
  100.         unset ARG[@] #init array to null
  101.         ARG=`echo -n "$@" | sed "s/ -/\n -/" | head -1`
  102.        
  103.   if [ "${#ARG[@]}" -ge 1 ];then
  104.         # if argument like "-v -v 24", throw the first -v
  105.         case "${ARG[0]}" in
  106.         --* | -*): ;;
  107.         *)
  108.         if [ "${#ARG[@]}" -ge 1 ];then
  109.           if [ "${#ARRAY_V[@]}" -ge 1 ];then
  110.             ARRAY_V=( "${ARRAY_V[@]}" "${ARG[@]}" )
  111.           else ARRAY_V=( "${ARG[@]}" )
  112.           fi
  113.           shift "${#ARG[@]}"       
  114.         fi
  115. #        echo "ARRAY_V = '${ARRAY_V[@]}'"
  116.         esac
  117.   fi  
  118.         continue
  119.         ;;
  120.   -*):
  121.         shift; continue
  122.          ;;
  123.   esac
  124. # Case of "-gl | -l | -v)" end
  125.         ;;
  126. ##--------------------------
  127.     -*)
  128.         echo "Unrecognized option \`$option'" 1>&2
  129.         #exit 1
  130.         ;;
  131.     esac
  132. done
  133. # ,FUZZY
  134. ##--------------------------
  135.         echo
  136.         echo "----"
  137.         echo "ARRAY_GL = '${ARRAY_GL[@]}'"
  138.         echo "ARRAY_L = '${ARRAY_L[@]}'"
  139.         echo "ARRAY_V = '${ARRAY_V[@]}'"
  140. exit 0;
  141. #
  142. # -gl gl1 gl2 gl3 -v v10 v11 -l l1 l2 -l l2 -l l3  -gl gl2 -v v21 gl
  143. ### output:
  144. #----
  145. # ARRAY_GL = 'gl1 gl2 gl3'
  146. # ARRAY_L = ''
  147. # ARRAY_V = 'v10 v11'
Copy the Code
Reply View the author
fasheng
deepin
2013-10-18 05:04
#2
试试 getoptgetopts 这两个命令
Reply View the author