问题描述
此处给出的答案与我的任何问题无关: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
- 如何传递多个标志?
For example runner.sh -c classname -t tagname
我尝试在ct
中使用c|t
、getopts
,但没有奏效。 - 如何为一个标志传递多个参数?
For example runner.sh -g arg1 arg2
- 如果我不提供任何标志,有没有办法
它将默认运行语句。我尝试使用
*
但是这种方法有一个问题。如果错误的标志是 前提是它将始终运行默认语句。
解决方法
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