在使用Cli工具时遇到了一个有趣的脚本问题
-a指的是这个cli工具中的参数
在Put或Post的情况下,我必须通过两个论点。所以:
--verb "$1" \
-a "$2" \
-a "$3"传入脚本
"put" "[\"name\"]" "testing this"工作得很好!
那么,在Get的情况下,我必须传递一个论点。所以:
--verb "$1" \
-a "$2" \
-a "$3"传入脚本
"get" "[\"name\"]"当然,这将失败,因为我必须通过一个论点,但我确实通过了两个,既然现在只赞成PUT操作,您认为我如何处理这个问题,使两个PUT和得到工作?
这都是在巴什
发布于 2022-10-30 15:13:29
对于更一般的案例处理(也将正确地处理三个以上的参数),请构造一个数组:
#!/usr/bin/env bash
case $BASH_VERSION in '') echo "ERROR: This must be run with bash, not sh" >&2; exit 1;; esac
# unconditionally, we always have a verb; assign to a variable then shift away
args=( --verb "$1" ) # this creates an array
shift # makes old $2 be $1, old $3 be $2, etc
# iterate over remaining arguments and add each preceded by '-a'
for arg in "$@"; do # iterate over all args left after the shift
args+=( -a "$arg" ) # for each, add '-a' then that arg to our array
done
# use the constructed array
runYourProgramWith "${args[@]}"仅正确处理$3是可选的:
--verb "$1" \
-a "$2" \
${3+ -a "$3" }https://stackoverflow.com/questions/74254195
复制相似问题