bash - 无法在 bash 中读取值多字命令行参数
问题描述
我正在编写一个具有 4 个命令行参数的 bash shell 脚本。3/4 参数是一个词,1/4 是多词。我设法获得了其中 2 个的值,但无法获得第三个和第四个的正确值。但是,如果我删除多工作参数,它会起作用。
options=$(getopt -l "help,env:,site:,cluster:,cluster-group:" -o "he:s:c:cg:" -a -- "$@")
eval set -- "$options"
while true
do
case $1 in
-h|--help)
showHelp
exit 0
;;
-e|--env)
shift
export environment=$1
;;
-s|--site)
shift
export site=$1
;;
-c|--cluster)
shift
export cluster=$1
;;
-cg|--cluster-group)
shift
export cluster_group=$1
;;
--)
shift
break;;
esac
shift
done
echo $environment
echo $site
echo $cluster
echo $cluster_group
运行时sh b.sh -s S1 -e E1 -c C1 -cg CG1
,输出为
E1
S1
g
我在这里做错了什么?
解决方案
As per man getopt(1)
:-o
只识别one-character
选项。
-o, --options shortopts
The short (one-character) options to be recognized. If
this option is not found, the first parameter of getopt
that does not start with a '-' (and is not an option
argument) is used as the short options string. Each short
option character in shortopts may be followed by one colon
to indicate it has a required argument, and by two colons
to indicate it has an optional argument. The first
character of shortopts may be '+' or '-' to influence the
way options are parsed and output is generated (see
section SCANNING MODES for details).
因此,在您的情况下,您只能提及一个字母,例如:g
在.-o
cluster-group
options=$(getopt -l "help:,env:,site:,cluster:,cluster-group:" -o "h:e:s:c:g:" -a -- "$@")
推荐阅读
- r - R 在 Ubuntu 20.04 上缺少 libreadline.so.7,不再工作
- python-3.x - 日期/时间 输入有效的日期/时间表单验证
- shell - sed:保留包含块模式的每个单词的最后一块
- sql - 将连接的行变成列
- unity3d - 'GoogleApiException: 参数“spreadsheetId”丢失' 尝试访问 Google 表格 API 的异常
- angular - 如何使用角度按位检索各自的值
- python - 如何使用 Beautiful Soup 在 Python 中获取 span 标签内的文本
- arrays - 将数字数组拆分为具有接近值的几个部分
- vue.js - Getters 不工作,状态值不能在 Vuex 中改变
- xamarin - Xamarin Android System.TypeLoadException 后清除 Azure DevOps 应用程序构建上的存储