如何在shell脚本的getopts中传递多个标志和多个参数?

问题描述

此处给出的答案与我的任何问题无关:bash getopts with multiple and mandatory options

我是 shell 脚本的新手,正在尝试使用 getopts 编写 shell 脚本,同时我找不到以下问题的答案。有人可以帮忙吗?

这是我开始编写的脚本:

 #!/bin/bash
  while getopts c:t:x: option ; do
  case $option in
    c)
      java -jar $file --fail-if-no-tests --include-classname "$OPTARG" --scan-class-path $file --include-tag regression
      ;;
    t)
      java -jar $file --fail-if-no-tests --include-classname '.*' --scan-class-path $file --include-tag "$OPTARG"
      ;;
    c | t)
      java -jar $file --fail-if-no-tests --include-classname "$OPTARG" --scan-class-path $file --include-tag "$OPTARG"
      ;;
    *)
      usage
  esac
  done
  1. 如何传递多个标志? For example runner.sh -c classname -t tagname 我尝试在 ct 中使用 c|tgetopts,但没有奏效。
  2. 如何为一个标志传递多个参数? For example runner.sh -g arg1 arg2
  3. 如果我不提供任何标志,有没有办法 它将认运行语句。我尝试使用 * 但是这种方法一个问题。如果错误的标志是 前提是它将始终运行认语句。

解决方法

while getopts 'c:t:x:' option; do
    case "$option" in
        c ) classname="$OPTARG" ;;
        t ) tagname="$OPTARG" ;;
        x ) xvar="$OPTARG" ;;
    esac
done

## now you can use the variables in your command