70

我需要帮助getopts

我创建了一个运行时看起来像这样的 Bash 脚本:

$ foo.sh -i env -d 目录 -s 子目录 -f 文件

在处理每个标志的一个参数时,它可以正常工作。但是当我从每个标志调用几个参数时,我不确定如何从getopts.

while getopts ":i:d:s:f:" opt
   do
     case $opt in
        i ) initial=$OPTARG;;
        d ) dir=$OPTARG;;
        s ) sub=$OPTARG;;
        f ) files=$OPTARG;;

     esac
done

获取选项后,我想从变量构建目录结构

foo.sh -i test -d directory -s subdirectory -s subdirectory2 -f file1 file2 file3

那么目录结构将是

/test/directory/subdirectory/file1
/test/directory/subdirectory/file2
/test/directory/subdirectory/file3
/test/directory/subdirectory2/file1
/test/directory/subdirectory2/file2
/test/directory/subdirectory2/file3

有任何想法吗?

4

11 回答 11

100

您可以多次使用相同的选项并将所有值添加到数组中。

对于这里非常具体的原始问题,Ryan 的mkdir -p解决方案显然是最好的。

但是,对于使用 getopts 从同一选项获取多个值的更一般的问题,这里是:

#!/bin/bash

while getopts "m:" opt; do
    case $opt in
        m) multi+=("$OPTARG");;
        #...
    esac
done
shift $((OPTIND -1))

echo "The first value of the array 'multi' is '$multi'"
echo "The whole list of values is '${multi[@]}'"

echo "Or:"

for val in "${multi[@]}"; do
    echo " - $val"
done

输出将是:

$ /tmp/t
The first value of the array 'multi' is ''
The whole list of values is ''
Or:

$ /tmp/t -m "one arg with spaces"
The first value of the array 'multi' is 'one arg with spaces'
The whole list of values is 'one arg with spaces'
Or:
 - one arg with spaces

$ /tmp/t -m one -m "second argument" -m three
The first value of the array 'multi' is 'one'
The whole list of values is 'one second argument three'
Or:
 - one
 - second argument
 - three
于 2013-12-24T13:32:47.397 回答
27

我知道这个问题很老,但我想把这个答案放在这里,以防有人来寻找答案。

像 BASH 这样的 Shell 已经支持像这样递归地创建目录,因此实际上并不需要脚本。例如,原始海报想要这样的东西:

$ foo.sh -i test -d directory -s subdirectory -s subdirectory2 -f file1 file2 file3
/test/directory/subdirectory/file1
/test/directory/subdirectory/file2
/test/directory/subdirectory/file3
/test/directory/subdirectory2/file1
/test/directory/subdirectory2/file2
/test/directory/subdirectory2/file3

这很容易用这个命令行完成:

pong:~/tmp
[10] rmclean$ mkdir -pv test/directory/{subdirectory,subdirectory2}/{file1,file2,file3}
mkdir: created directory ‘test’
mkdir: created directory ‘test/directory’
mkdir: created directory ‘test/directory/subdirectory’
mkdir: created directory ‘test/directory/subdirectory/file1’
mkdir: created directory ‘test/directory/subdirectory/file2’
mkdir: created directory ‘test/directory/subdirectory/file3’
mkdir: created directory ‘test/directory/subdirectory2’
mkdir: created directory ‘test/directory/subdirectory2/file1’
mkdir: created directory ‘test/directory/subdirectory2/file2’
mkdir: created directory ‘test/directory/subdirectory2/file3’

甚至更短一点:

pong:~/tmp
[12] rmclean$ mkdir -pv test/directory/{subdirectory,subdirectory2}/file{1,2,3}
mkdir: created directory ‘test’
mkdir: created directory ‘test/directory’
mkdir: created directory ‘test/directory/subdirectory’
mkdir: created directory ‘test/directory/subdirectory/file1’
mkdir: created directory ‘test/directory/subdirectory/file2’
mkdir: created directory ‘test/directory/subdirectory/file3’
mkdir: created directory ‘test/directory/subdirectory2’
mkdir: created directory ‘test/directory/subdirectory2/file1’
mkdir: created directory ‘test/directory/subdirectory2/file2’
mkdir: created directory ‘test/directory/subdirectory2/file3’

或者更短,更符合:

pong:~/tmp
[14] rmclean$ mkdir -pv test/directory/subdirectory{1,2}/file{1,2,3}
mkdir: created directory ‘test’
mkdir: created directory ‘test/directory’
mkdir: created directory ‘test/directory/subdirectory1’
mkdir: created directory ‘test/directory/subdirectory1/file1’
mkdir: created directory ‘test/directory/subdirectory1/file2’
mkdir: created directory ‘test/directory/subdirectory1/file3’
mkdir: created directory ‘test/directory/subdirectory2’
mkdir: created directory ‘test/directory/subdirectory2/file1’
mkdir: created directory ‘test/directory/subdirectory2/file2’
mkdir: created directory ‘test/directory/subdirectory2/file3’

或者最后,使用序列:

pong:~/tmp
[16] rmclean$ mkdir -pv test/directory/subdirectory{1..2}/file{1..3}
mkdir: created directory ‘test’
mkdir: created directory ‘test/directory’
mkdir: created directory ‘test/directory/subdirectory1’
mkdir: created directory ‘test/directory/subdirectory1/file1’
mkdir: created directory ‘test/directory/subdirectory1/file2’
mkdir: created directory ‘test/directory/subdirectory1/file3’
mkdir: created directory ‘test/directory/subdirectory2’
mkdir: created directory ‘test/directory/subdirectory2/file1’
mkdir: created directory ‘test/directory/subdirectory2/file2’
mkdir: created directory ‘test/directory/subdirectory2/file3’
于 2012-05-30T14:10:21.050 回答
25

getopts 选项只能接受零个或一个参数。您可能希望更改界面以删除 -f 选项,并仅迭代剩余的非选项参数

usage: foo.sh -i end -d dir -s subdir file [...]

所以,

while getopts ":i:d:s:" opt; do
  case "$opt" in
    i) initial=$OPTARG ;;
    d) dir=$OPTARG ;;
    s) sub=$OPTARG ;;
  esac
done
shift $(( OPTIND - 1 ))

path="/$initial/$dir/$sub"
mkdir -p "$path"

for file in "$@"; do
  touch "$path/$file"
done
于 2011-09-23T14:10:22.637 回答
13

我解决了你遇到的同样的问题:

代替:

foo.sh -i test -d directory -s subdirectory -s subdirectory2 -f file1 file2 file3

做这个:

foo.sh -i test -d directory -s "subdirectory subdirectory2" -f "file1 file2 file3"

使用空格分隔符,您可以通过基本循环运行它。这是代码:

while getopts ":i:d:s:f:" opt
   do
     case $opt in
        i ) initial=$OPTARG;;
        d ) dir=$OPTARG;;
        s ) sub=$OPTARG;;
        f ) files=$OPTARG;;

     esac
done

for subdir in $sub;do
   for file in $files;do
      echo $subdir/$file
   done   
done

这是一个示例输出:

$ ./getopts.sh -s "testdir1 testdir2" -f "file1 file2 file3"
testdir1/file1
testdir1/file2
testdir1/file3
testdir2/file1
testdir2/file2
testdir2/file3
于 2011-12-28T15:54:09.420 回答
11

如果你想为一个选项指定任意数量的值,你可以使用一个简单的循环来找到它们并将它们填充到一个数组中。例如,让我们修改 OP 的示例以允许任意数量的 -s 参数:

unset -v sub
while getopts ":i:d:s:f:" opt
   do
     case $opt in
        i ) initial=$OPTARG;;
        d ) dir=$OPTARG;;
        s ) sub=("$OPTARG")
            until [[ $(eval "echo \${$OPTIND}") =~ ^-.* ]] || [ -z $(eval "echo \${$OPTIND}") ]; do
                sub+=($(eval "echo \${$OPTIND}"))
                OPTIND=$((OPTIND + 1))
            done
            ;;
        f ) files=$OPTARG;;
     esac
done

这需要第一个参数 ($OPTARG) 并将其放入数组 $sub。然后它将继续搜索剩余的参数,直到它遇到另一个虚线参数或没有更多参数要评估。如果它找到更多不是虚线参数的参数,它会将其添加到 $sub 数组并增加 $OPTIND 变量。

因此,在 OP 的示例中,可以运行以下命令:

foo.sh -i test -d directory -s subdirectory1 subdirectory2 -f file1

如果我们将这些行添加到脚本中来演示:

echo ${sub[@]}
echo ${sub[1]}
echo $files

输出将是:

subdirectory1 subdirectory2
subdirectory2
file1
于 2017-03-16T17:11:26.687 回答
7

实际上有一种方法可以使用 ' 检索多个参数,但它需要使用'变量getopts进行一些手动修改。getoptsOPTIND

请参阅以下脚本(转载如下):https ://gist.github.com/achalddave/290f7fcad89a0d7c3719 。可能有一种更简单的方法,但这是我能找到的最快的方法。

#!/bin/sh

usage() {
cat << EOF
$0 -a <a1> <a2> <a3> [-b] <b1> [-c]
    -a      First flag; takes in 3 arguments
    -b      Second flag; takes in 1 argument
    -c      Third flag; takes in no arguments
EOF
}

is_flag() {
    # Check if $1 is a flag; e.g. "-b"
    [[ "$1" =~ -.* ]] && return 0 || return 1
}

# Note:
# For a, we fool getopts into thinking a doesn't take in an argument
# For b, we can just use getopts normal behavior to take in an argument
while getopts "ab:c" opt ; do
    case "${opt}" in
        a)
            # This is the tricky part.

            # $OPTIND has the index of the _next_ parameter; so "\${$((OPTIND))}"
            # will give us, e.g., ${2}. Use eval to get the value in ${2}.
            # The {} are needed in general for the possible case of multiple digits.

            eval "a1=\${$((OPTIND))}"
            eval "a2=\${$((OPTIND+1))}"
            eval "a3=\${$((OPTIND+2))}"

            # Note: We need to check that we're still in bounds, and that
            # a1,a2,a3 aren't flags. e.g.
            #   ./getopts-multiple.sh -a 1 2 -b
            # should error, and not set a3 to be -b.
            if [ $((OPTIND+2)) -gt $# ] || is_flag "$a1" || is_flag "$a2" || is_flag "$a3"
            then
                usage
                echo
                echo "-a requires 3 arguments!"
                exit
            fi

            echo "-a has arguments $a1, $a2, $a3"

            # "shift" getopts' index
            OPTIND=$((OPTIND+3))
            ;;
        b)
            # Can get the argument from getopts directly
            echo "-b has argument $OPTARG"
            ;;
        c)
            # No arguments, life goes on
            echo "-c"
            ;;
    esac
done
于 2014-03-27T18:24:57.903 回答
2

最初的问题涉及 getopts,但还有另一种解决方案可以在没有 getopts 的情况下提供更灵活的功能(这可能有点冗长,但提供了更灵活的命令行界面)。这是一个例子:

while [[ $# > 0 ]]
do
    key="$1"
    case $key in
        -f|--foo)
            nextArg="$2"
            while ! [[ "$nextArg" =~ -.* ]] && [[ $# > 1 ]]; do
                case $nextArg in
                    bar)
                        echo "--foo bar found!"
                    ;;
                    baz)
                        echo "--foo baz found!"
                    ;;
                    *)
                        echo "$key $nextArg found!"
                    ;;
                esac
                if ! [[ "$2" =~ -.* ]]; then
                    shift
                    nextArg="$2"
                else
                    shift
                    break
                fi
            done
        ;;
        -b|--bar)
            nextArg="$2"
            while ! [[ "$nextArg" =~ -.* ]] && [[ $# > 1 ]]; do
                case $nextArg in
                    foo)
                        echo "--bar foo found!"
                    ;;
                    baz)
                        echo "--bar baz found!"
                    ;;
                    *)
                        echo "$key $nextArg found!"
                    ;;
                esac
                if ! [[ "$2" =~ -.* ]]; then
                    shift
                    nextArg="$2"
                else
                    shift
                    break
                fi
            done
        ;;
        -z|--baz)
            nextArg="$2"
            while ! [[ "$nextArg" =~ -.* ]] && [[ $# > 1 ]]; do

                echo "Doing some random task with $key $nextArg"

                if ! [[ "$2" =~ -.* ]]; then
                    shift
                    nextArg="$2"
                else
                    shift
                    break
                fi
            done
        ;;
        *)
            echo "Unknown flag $key"
        ;;
    esac
    shift
done

在这个例子中,我们循环遍历所有命令行选项,寻找与我们接受的命令行标志(例如 -f 或 --foo)相匹配的参数。一旦我们找到一个标志,我们就会遍历每个参数,直到我们用完参数或遇到另一个标志。这让我们回到了只处理标志的外循环。

使用此设置,以下命令是等效的:

script -f foo bar baz
script -f foo -f bar -f baz
script --foo foo -f bar baz
script --foo foo bar -f baz

您还可以解析令人难以置信的杂乱无章的参数集,例如:

script -f baz derp --baz herp -z derp -b foo --foo bar -q llama --bar fight

要获得输出:

--foo baz found!
-f derp found!
Doing some random task with --baz herp
Doing some random task with -z derp
--bar foo found!
--foo bar found!
Unknown flag -q
Unknown flag llama
--bar fight found!
于 2016-09-28T18:21:17.877 回答
2
#!/bin/bash
myname=$(basename "$0")

# help function
help () { cat <<EOP
   $myname: -c cluster [...] -a action [...] -i instance [...]
EOP
}

# parse sub options
get_opts () {
  rs='' && rc=0 # return string and return code
  while [[ $# -gt 0 ]]; do
    shift
    [[ "$1" =~ -.* ]] && break ||  rs="$rs $1" && rc=$((rc + 1))
  done
  echo "$rs"
}

#parse entire command-line
while [[ $# -gt 0 ]]; do
    case $1 in
        "-a") ACTS="$(get_opts $@)"
           ;;
        "-i") INSTS=$(get_opts $@)
           ;;
        "-c") CLUSTERS=$(get_opts $@)
           ;;
        "-h") help
           ;;
        ?) echo "sorry, I dont do $1"
           exit
           ;;
    esac
    shift
done
于 2019-01-11T23:17:17.383 回答
1

以下链接应该是此要求的通用解决方案。

它易于注入且足够清晰易懂,也最大限度地减少了对原始代码的影响。

使用 getopts (bash) 的多个选项参数

function getopts-extra () {
    declare i=1
    # if the next argument is not an option, then append it to array OPTARG
    while [[ ${OPTIND} -le $# && ${!OPTIND:0:1} != '-' ]]; do
        OPTARG[i]=${!OPTIND}
        let i++ OPTIND++
    done
}

# Use it within the context of `getopts`:
while getopts s: opt; do
    case $opt in
       s) getopts-extra "$@"
          args=( "${OPTARG[@]}" )
    esac
done
于 2021-03-21T15:18:22.107 回答
0

因为你没有展示你希望如何构建你的列表

/test/directory/subdirectory/file1
. . .
test/directory/subdirectory2/file3

有点不清楚如何继续,但基本上你需要继续将任何新值附加到适当的变量,即

 case $opt in
    d ) dirList="${dirList} $OPTARG" ;;
 esac

请注意,在第一次通过时 dir 将为空,并且您最终会在${dirList}. (如果你真的需要前面或后面不包含任何额外空格的代码,我可以给你看一个命令,但它会很难理解,而且这里似乎不需要它,但让我知道)

然后,您可以将列表变量包装在 for 循环中以发出所有值,即

for dir in ${dirList} do
   for f in ${fileList} ; do
      echo $dir/$f
   done
done

最后,将任何未知输入“捕获”到您的案例陈述中被认为是一种很好的做法,即

 case $opt in
    i ) initial=$OPTARG;;
    d ) dir=$OPTARG;;
    s ) sub=$OPTARG;;
    f ) files=$OPTARG;;
    * ) 
       printf "unknown flag supplied "${OPTARG}\nUsageMessageGoesHere\n"
       exit 1
    ;;

 esac 

我希望这有帮助。

于 2011-09-23T13:58:28.633 回答
0

这是为单个选项传递多个参数的简单方法。

#!/bin/bash

#test.sh -i 'input1 input2'
#OR
#test.sh -i 'input*'

while getopts "i:" opt; do
    case $opt in
        i ) input=$OPTARG;;
    esac
done
inputs=( $input )

echo "First input is "$inputs""
echo "Second input is "${inputs[1]}""
echo "All inputs: "${inputs[@]}""
于 2021-11-17T18:17:23.580 回答