gpt4 book ai didi

linux - 在 bash 中获取参数

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:11 25 4
gpt4 key购买 nike

我需要在 bash 中获取参数,我可以获取参数 h,c,p,s ,a 但我不能接受参数 b

为什么我不能接受?这是我的脚本:

if [ ! -z $1 ]; then
HOSTNAME=""
CLIENT=""
SUBSCRIPTIONS_GROUPS=""
PROVIDER=""
SERVER=""
while getopts ":h:c:p:s:a:b" opt; do
case $opt in
h) HOSTNAME=${OPTARG}
;;
c) CLIENT=${OPTARG}
;;
p) PROVIDER=${OPTARG}
;;
s) SUBSCRIPTIONS_GROUPS=${OPTARG}
;;
a) ALWAYS_ON="on"
;;
b) SERVER=${OPTARG}
;;
?) ;;
esac
done
fi

最佳答案

我发现您的实现存在两个缺陷:

  • 第一个字符是冒号
  • b之后没有冒号

您的 getops 字符串应如下所示:

while getopts "h:c:p:s:ab:" opt; do
...

When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag

If the very first character of the option-string is a : (colon), which would normally be nonsense because there's no option letter preceding it, getopts switches to "silent error reporting mode". In productive scripts, this is usually what you want because it allows you to handle errors yourself without being disturbed by annoying messages.

摘自 http://wiki.bash-hackers.org/howto/getopts_tutorial

关于linux - 在 bash 中获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com