我想向脚本传递长参数,并参考此链接。我创建了这个 my_script:
#!/usr/bin/env bash
#
# Adapted from https://www.shellscript.sh/examples/getopt/
#
set -euo pipefail
user_type=unset
user_id=unset
country=unset
dev_env=unset
position_id=unset
usage(){
>&2 cat << EOF
Usage: $0]
[ -a | --user_type input
[ -b | --user_id input ]
[ -c | --country input ]
[ -d | --dev_env input ]
[ -e | --position_id input ]
EOF
exit 1
}
>&2 echo [$@] passed to script
args=$(getopt -a -o ha:b:c:d: --long help,user_type:,user_id:,country:,dev_env:,position_id: -- "$@")
if [[ $? -gt 0 ]]; then
usage
fi
>&2 echo getopt creates [${args}]
eval set -- ${args}
while :
do
case $1 in
-a | --user_type) user_type=$2 ; shift 2 ;;
-b | --user_id) user_id=$2 ; shift 2 ;;
-h | --help) usage ; shift ;;
-c | --country) country=$2 ; shift 2 ;;
-d | --dev_env) dev_env=$2 ; shift 2 ;;
-e | --position_id) position_id=$2 ; shift 2 ;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift; break ;;
*) >&2 echo Unsupported option: $1
usage ;;
esac
done
if [[ $# -eq 0 ]]; then
usage
fi
>&2 echo "user_type : ${user_type}"
>&2 echo "user_id : ${user_id} "
>&2 echo "country : ${country}"
>&2 echo "dev_env : ${dev_env}"
>&2 echo "position_id : ${position_id}"
>&2 echo "$# parameter/s remaining: $@"
>&2 echo "Looping through remaining parameter/s:"
# set input field separator to keep quoted parameters together
# for example, "my input" will remain as one input
IFS=$'\n'
for param in $@; do
>&2 echo -e "\t${param}"
done
echo "user ${user_type} with user id ${user_id} has been created with position_id $position_id"
exit 0
并在终端上尝试运行它:
bash test_bash --user_type abc --user_id a1b2 --country aud --dev_env uat --position_id aFWf
我收到的帮助信息如下:
[--user_type abc --user_id a1b2 --country aud --dev_env uat --position_id aFWf] passed to script
getopt creates [ --user_type 'abc' --user_id 'a1b2' --country 'aus' --dev_env 'uat' --position_id 'aFWf' --]
Usage: test_bash]
[ -a | --user_type input
[ -b | --user_id input ]
[ -c | --country input ]
[ -d | --dev_env input ]
[ -e | --position_id input ]
我尝试更新/更改一些东西,比如 args=getopt 和其他几个选项。但我的参数没有被脚本解析。提前感谢您的帮助。谢谢。
这是你的代码的工作版本。我会时不时地尝试解释我为什么改变了这些内容
我更喜欢将变量初始化为空值,然后在必要时替换错误消息中的文本。你
user_type=unset
可以将变量设置为文本,unset
但它会阻止以后的简单测试,例如if [ -z "$user_type ]; then echo "the variable user_type has no value"; fi
,如果用户提供unset
文字值,则肯定会中断:没有意义使用
$@
,因为 (a) 你没有用双引号括起来,并且 (b) 你写的是字符串而不是一系列值。(如果你不明白这一点,请查找、和之间的区别)。最后,我告诉你$*
代表$@
你的程序报告错误。构造会为你挑选程序名称:"$@"
getopt
${0##*/}
不要忘记双引号
$args
:上面的语句
shift
在处理参数时会消耗它们。但一会儿你想打印剩余的参数,所以如果没有剩余参数,那么使用错误就毫无意义了。代码已删除。另外,我已经删除了中的错误条件,
case
因为它已经由直接处理getopt
。在下一节中,诸如 之类的变量
$user_type
可能未设置,因此它们没有值。如果值未设置/为空,则${user_type:-<unset>}
显示以下内容:<unset>
双引号,
"$@"
这样即使单词最初包含空格,也会被视为单个元素:最后,如果代码在文件 中
myscript
,请不要将其作为 运行bash myscript
。而是将文件设置为可读/可执行 (chmod a+rx myscript
) 并直接运行它 ( )。第一行告诉系统要使用哪个解释器,因此运行时./myscript
无需明确指定。bash
例子:
好吧,让我们来解决这个问题。抱歉,之前的回答虽然正确,但我当时在火车上,没有机会很好地解释我的解决方案。
她的代码已编辑,可以按预期工作:
结果如下:
正如我在上一个回答中所说,第二个 if 语句是错误的,因为此时没有更多参数,因此
$#
对应于0
并且代码以状态 1 从usage
函数退出。但在此之前,请仔细检查您的代码,*) >&2 echo Unsupported option: $1
在我看来,这也是以同样的方式破坏您的代码的原因,因为--
在getopt
方法中。您的代码充满了管理给定参数的方法,这通常不是一个好主意。我的建议是尽可能保持代码干净。以下是您代码中真正有用的实现相同结果的方法:
结果如下: