Bash 脚本与 getopt 落入“默认”第二个参数

Bash 脚本与 getopt 落入“默认”第二个参数

我正在重新学习getoptbash 中的一个小脚本,但第二个参数属于case.

#! /bin/bash

LONG_OPTION_LIST=(
    "arg-a"
    "arg-b:"
    "arg-c:"
)
SORT_OPTION_LIST=(
    "a"
    "b:"
    "c:"
)
# Read the parameters
opts=$(getopt -q \
  --longoptions "$(printf "%s," "${LONG_OPTION_LIST[@]}")" \
  --name "$(basename "$0")" \
  --options "$(printf "%s" "${SORT_OPTION_LIST[@]}")" \
  -- "$@"
)
eval set -- "$opts"

echo "##$1##"
echo "##$2##"
echo "##$3##"
echo "##$4##"
echo "##$5##"
echo "#########"

argA=0
# It it is same a queue (process the head) because $1 and $2
for arg
do
    echo $1
    echo $2
    echo "--------"
    case "$arg" in
        --arg-a | -a)
            argA=1
            shift 1
            ;;
        --arg-b | -b)
            argB=$2
            shift 2
            ;;
        --arg-c | -c)
            argC=$2
            shift 2
            ;;
        *)
            echo "###$1###"
            echo "break"
            echo "_________"
            break
            ;;
    esac
done

echo "argA $argA"
echo "argB $argB"
echo "argC $argC"

还有一些例子:

user@pc:/tmp$ ./test.bash -a
##-a##
##--##
####
####
####
#########
-a
--
--------
--

--------
###--###
break
_________
argA 1
argB 
argC 
user@pc:/tmp$ ./test.bash -b 111
##-b##
##111##
##--##
####
####
#########
-b
111
--------
--

--------
###--###
break
_________
argA 0
argB 111
argC 
user@pc:/tmp$ ./test.bash -a -b 111
##-a##
##-b##
##111##
##--##
####
#########
-a
-b
--------
-b
111
--------
--

--------
###--###
break
_________
argA 1
argB 111
argC 
user@pc:/tmp$ ./test.bash -b 111 -a
##-b##
##111##
##-a##
##--##
####
#########
-b
111
--------
-a
--
--------
###-a###
break
_________
argA 0
argB 111
argC 

答案1

for arg
do
    ...
    shift 1

我认为这里的移位不会像您希望的那样工作,循环将循环的单词将在循环开始时设置,并且其中的移位不会影响它。例如

$ set -- aa bb cc; 
$ for x; do echo $x; shift; done
aa
bb
cc

"$@"不过,在循环后将留空,因为它为每个元素移动一次。


getopt大多数使用循环的示例while true,查看$1and $2,手动移动并在看到终止符时结束循环--(util-linux getopt 总是添加终止符,但您也可以手动检查非选项):

参见例如

答案2

您不需要对该值进行转换。它已经自动完成了。实际上shift 2隐藏了下一个选项。

编写案例的标准方法是这样的:

for arg
do
    echo $1
    echo $2
    echo "--------"
    case "$arg" in
        --arg-a | -a)
            argA=1
            ;;
        --arg-b | -b)
            argB=$2
            ;;
        --arg-c | -c)
            argC=$2
            ;;
    esac
    shift
done

注意案件之后的单班制。

另外,在这种情况下你不应该放 * - 它会对任何选项做出反应。如果您需要对意外选项做出自定义反应,最好执行以下操作:

opts=$(getopt ... )
[ $? -eq 0 ] || { 
    echo "Known options are..."
    exit 1
}

答案3

for正如@ikkachu 所说,错误正在使用while,谢谢。

这是它使用的小脚本getopt

#! /bin/bash

#~ get_opt.example.sh
#~ Copyright (C) 2022 Miguel de Dios Matias

#~ This program is free software: you can redistribute it and/or modify
#~ it under the terms of the GNU General Public License as published by
#~ the Free Software Foundation, either version 3 of the License, or
#~ (at your option) any later version.

#~ This program is distributed in the hope that it will be useful,
#~ but WITHOUT ANY WARRANTY; without even the implied warranty of
#~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#~ GNU General Public License for more details.

#~ You should have received a copy of the GNU General Public License
#~ along with this program. If not, see <http://www.gnu.org/licenses/>.

: '
Examples the calls:

$ ./getopt.example.bash --arg-b 111 -a 2222 3333
argA 1
argB 111
argC 
argD 0
unamedOptions 2222 3333

'

function help() {
    echo "$0 [(--arg-a | -a)] [(--arg-b | -b) <data_b>] [(--arg-c | -c <data_c>)] [-d] [(--help | -h)]"
}

LONG_OPTION_LIST=(
    "arg-a"
    "arg-b:"
    "arg-c:"
    "help"
)
SORT_OPTION_LIST=(
    "a"
    "b:"
    "c:"
    "d"
    "h"
)
# Read the parameters
opts=$(getopt -q \
  --longoptions "$(printf "%s," "${LONG_OPTION_LIST[@]}")" \
  --name "$(basename "$0")" \
  --options "$(printf "%s" "${SORT_OPTION_LIST[@]}")" \
  -- "$@"
)
eval set -- "$opts"

argA=0
argD=0
unamedOptions=()
# It it is same a queue (process the head) because $1 and $2
while true
do
    case "$1" in
        --arg-a | -a)
            argA=1
            ;;
        --arg-b | -b)
            argB=$2
            shift 1
            ;;
        --arg-c | -c)
            argC=$2
            shift 1
            ;;
        -d)
            argD=1
            ;;
        --help | -h)
            help
            exit 0
            ;;
        --)
            # End options now the unamed options
            ;;
        *)
            unamedOptions+=("$1")
            ;;
    esac
    shift 1
    if [ $# -eq 0 ]
    then
        break
    fi
done

echo "argA $argA"
echo "argB $argB"
echo "argC $argC"
echo "argD $argD"
echo "unamedOptions ${unamedOptions[@]}"

相关内容