求助:bash中如何提取命令行选项的辅助参数

sh/bash/dash/ksh/zsh等Shell脚本
回复
careone2013
帖子: 1
注册时间: 2013-10-04 18:32

Re: 求助:bash中如何提取命令行选项的辅助参数

#1

帖子 careone2013 » 2013-10-04 19:27

接 1 楼
程序代码

代码: 全选

#! /bin/bash
## coding: utf-8
## translate Emacs/XEmacs menu to other languages
## updated by Careone <[email protected]>, 2013-10-04

VERSION='2.12' 

# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: $0 [OPTION]
toggle $NAMEU menu to special language, for current user or global.
  You also can translate $NAMEU menu to special language(s) if translate 
scripts existed. Please run '$0 -d' for translate/develop options.

  -V, --version    print the version information and exit
  -h, --help       print this message and exit

Translate and develop related options
usage:    $0 -l LANGUAGE -v VERSION
examples: $0 -l zh_CN -v 24.1
          $0 -l zh_CN -v 24.1 24.2
          $0 -l zh_CN zh_TW -v 24.1 24.2

      -gl [LANGUAGE_1 LANGUAGE_2 ...]    (root please!)
        with -v, (Global) translate $NAMEU lisp/*/*.el files to special 
        version(s). If no VERSION given, try probe current $NAMEU version
        installed instead.
      -l [LANGUAGE_1 LANGUAGE_2 ...]
        with -v, translate $NAMEU lisp/*/*.el files to special language(s)
      -v [VERSION1 VERSION2 ...]
        with -gl or -l, translate $NAMEU lisp/*/*.el files to special version(s). If
        no VERSION given, try probe current $NAMEU version installed instead.

      !!! NOTICE !!!
       * option '-v':
         translated .el files save to ~/$TRANSOF/usr/share/$NAMEL/... and
         apply to current user only

       * option '-gl': (root please!)
         translated .el files save to /usr/share/$NAMEL/... and apply to ALL
         USERS. If you just want to test your translations, please use option 
         '-l' instead!
	
EOF
}
###

# tag 2000
## PART 2: main
# Check the arguments.

for option in "$@"; do
    case "$option" in
    -h | --help)
	usage
	exit 0 ;;

## todo -------------------------
    -gl | -l | -v)
    declare -a ARRAY_GL
    declare -a ARRAY_L
    declare -a ARRAY_V
    declare -a ARG

## 2013-09-20
#ARGS="$@"
  case "$1" in
  -gl)
    	shift
	unset ARG[@] #init array to null
	ARG=( `echo -n "$@" | sed "s/ -/\n -/" | head -1` )

  if [ "${#ARG[@]}" -ge 1 ];then 
	# if argument like "-gl -gl zh_CN", throw the first -gl
	case "${ARG[0]}" in
	--* | -*): ;; 
	*)

	  if [ "${#ARRAY_GL[@]}" -ge 1 ];then 
	    ARRAY_GL=( "${ARRAY_GL[@]}" "${ARG[@]}" )
	  else ARRAY_GL=( "${ARG[@]}" )
	  fi
	  shift "${#ARG[@]}"	
#	echo "ARRAY_GL = '${ARRAY_GL[@]}'"
	esac
  fi  
 	continue
	;;

  -l)
    	shift
	unset ARG[@] #init array to null
	ARG=`echo -n "$@" | sed "s/ -/\n -/" | head -1`

  if [ "${#ARG[@]}" -ge 1 ];then 
	# if argument like "-l -l zh_CN", throw the first -l
	case "${ARG[0]}" in
	--* | -*): ;; 
	*)

	if [ "${#ARG[@]}" -ge 1 ];then 
  	  if [ "${#ARRAY_L[@]}" -ge 1 ];then 
	    ARRAY_L=( "${ARRAY_L[@]}" "${ARG[@]}" )
	  else ARRAY_L=( "${ARG[@]}" )
	  fi
	  shift "${#ARG[@]}"	
	fi
#	echo "ARRAY_L = '${ARRAY_L[@]}'"
	esac
  fi  
 	continue
	;;
  -v)
    	shift
	unset ARG[@] #init array to null
	ARG=`echo -n "$@" | sed "s/ -/\n -/" | head -1`
	
  if [ "${#ARG[@]}" -ge 1 ];then 
	# if argument like "-v -v 24", throw the first -v
	case "${ARG[0]}" in
	--* | -*): ;; 
	*)

	if [ "${#ARG[@]}" -ge 1 ];then 
	  if [ "${#ARRAY_V[@]}" -ge 1 ];then 
	    ARRAY_V=( "${ARRAY_V[@]}" "${ARG[@]}" )
	  else ARRAY_V=( "${ARG[@]}" )
	  fi
	  shift "${#ARG[@]}"	
	fi
#	echo "ARRAY_V = '${ARRAY_V[@]}'"
	esac
  fi  
 	continue
 	;;
  -*):
	shift; continue
	 ;;
  esac
# Case of "-gl | -l | -v)" end
	;; 
##--------------------------
    -*)
	echo "Unrecognized option \`$option'" 1>&2
	#exit 1
	;;
    esac

done

# ,FUZZY
##--------------------------
	echo
	echo "----"
	echo "ARRAY_GL = '${ARRAY_GL[@]}'"
	echo "ARRAY_L = '${ARRAY_L[@]}'"
	echo "ARRAY_V = '${ARRAY_V[@]}'"
exit 0;

#
# -gl gl1 gl2 gl3 -v v10 v11 -l l1 l2 -l l2 -l l3  -gl gl2 -v v21 gl
### output:
#----
# ARRAY_GL = 'gl1 gl2 gl3'
# ARRAY_L = ''
# ARRAY_V = 'v10 v11'
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 求助:bash中如何提取命令行选项的辅助参数

#2

帖子 eexpress » 2013-10-04 20:45

● 鸣学
回复