Bash - getopts - 在命令行参数传递的第一个位置的质量参数(操作数)

Wak*_*nka 6 bash parameter-passing getopts command-line-arguments

我使用以下行(希望这是最好的做法,如果不能纠正我)来处理命令行选项:

#!/usr/bin/bash

read -r -d '' HELP <<EOF
OPTIONS:
-c    Enable color output
-d    Enable debug output
-v    Enable verbose output
-n    Only download files (mutually exclusive with -r)
-r    Only remove files (mutually exclusive with -n)
-h    Display this help
EOF

# DECLARE VARIABLES WITH DEFAULT VALUES
color=0
debug=0
verbose=0
download=0
remove=0


OPTIND=1                # Reset in case getopts has been used previously in the shell
invalid_options=();     # Array for invalid options

while getopts ":cdvnrh" opt; do
  echo "Actual opt: $opt"
  case $opt in
    c)
      color=1
      ;;
    d)
      debug=1
      ;;
    v)
      verbose=1
      ;;
    n)
      download=1
      ;;
    r)
      remove=1
      ;;
    h)
      echo "$HELP"
      exit 1
      ;;
   \?)
      invalid_options+=($OPTARG)
      ;;
   *)
      invalid_options+=($OPTARG)
      ;;
  esac
done

# HANDLE INVALID OPTIONS
if [ ${#invalid_options[@]} -ne 0 ]; then
  echo "Invalid option(s):" >&2
  for i in "${invalid_options[@]}"; do
    echo $i >&2
  done
  echo "" >&2
  echo "$HELP" >&2
  exit 1
fi

# SET $1 TO FIRST MASS ARGUMENT, $2 TO SECOND MASS ARGUMENT ETC
shift $((OPTIND - 1))

# HANDLE CORRECT NUMBER OF MASS OPTIONS
if [ $# -ne 2 ]; then
  echo "Correct number of mass arguments are 2"
  echo "" >&2
  echo "$HELP" >&2
  exit 1
fi


# HANDLE MUTUALLY EXCLUSIVE OPTIONS
if [ $download -eq 1 ] && [ $remove -eq 1 ]; then
  echo "Options for download and remove are mutually exclusive" >&2
  echo "$HELP" >&2
  exit 1
fi



echo "color:    $color"
echo "debug:    $debug"
echo "verbose:  $verbose"
echo "download: $download"
echo "remove:   $remove"
echo "\$1:      $1"
echo "\$2:      $2"
Run Code Online (Sandbox Code Playgroud)

如果我调用脚本的方式,质量参数(那些不是开关的开关或参数)是最后一个参数,一切都正常工作:

$ ./getopts.sh -c -d -v -r a b
Actual opt: c
Actual opt: d
Actual opt: v
Actual opt: r
color:    1
debug:    1
verbose:  1
download: 0
remove:   1
$1:      a
$2:      b
Run Code Online (Sandbox Code Playgroud)

问题是,当我想调用脚本时,质量参数是第一个(或者在不使用参数的开关中间的某个地方)

$ ./getopts.sh a b -c -d -v -r
Correct number of mass arguments are 2

OPTIONS:
-c    Enable color output
-d    Enable debug output
-v    Enable verbose output
-n    Only download files (mutually exclusive with -r)
-r    Only remove files (mutually exclusive with -d)
-h    Display this help
Run Code Online (Sandbox Code Playgroud)

要么

$ ./getopts.sh -c a b -d -v -r
Actual opt: c
Correct number of mass arguments are 2

OPTIONS:
-c    Enable color output
-d    Enable debug output
-v    Enable verbose output
-n    Only download files (mutually exclusive with -r)
-r    Only remove files (mutually exclusive with -d)
-h    Display this help
Run Code Online (Sandbox Code Playgroud)

我认为这应该是(POSIX)标准,因为基本相同的语法在我的系统上按预期工作:

$ cp test1/ test2/ -r
$ cp test1/ -r test2/
Run Code Online (Sandbox Code Playgroud)

我有搜索在互联网上,但只有一件事情是接近我的问题是这样一个关系到C.

Zab*_*ula 1

Bash 提供了两种参数解析方法。

内置命令getopts是一种更新的、易于使用的机制来解析参数,但它不是很灵活。getopts不允许混合选项和质量参数。

外部命令getopt是一种较旧且更复杂的参数解析机制。它允许长/短选项,并且 gnu 扩展允许混合选项和质量参数。