为了账号安全,请及时绑定邮箱和手机立即绑定

关于getopt语法错误

关于getopt语法错误

明月笑刀无情 2021-04-11 20:17:30
好吧,我是一个getops和b一起玩的linux bash业余爱好者getop。我已经在几个论坛上阅读了有关该主题的几次对话,但似乎无法使我的代码正常工作。这是一个使用的小脚本getopts,可从该论坛回收:#!bin/bashwhile getopts ":a:p:" opt; do  case $opt in    a) arg_1="$OPTARG"    ;;    p) arg_2="$OPTARG"    ;;    \?)    ;;  esacdoneprintf "Argument firstArg is %s\n" "$arg_1"printf "Argument secondArg is %s\n" "$arg_2"它的工作是:bash test02.sh -asomestring -bsomestring2 #either with or without quotes#Argument firstArg is somestring#Argument secondArg is somestring2现在,由于我想尝试使用长选项名称,因此我正在尝试getopt,试图从网上发现的示例中理解语法:#!/bin/bashtemp=`getopt -o a:b: -l arga:,argb:--"$@"`eval set --"$temp"while true ; do  case "$1" in    a|arga) firstArg="$OPTARG"    ;;    b|argb) secondArg="$OPTARG"    ;;    \?)    ;;  esacdoneprintf "Argument firstArg is %s\n" "$firstArg"printf "Argument secondArg is %s\n" "$secondArg"上面的代码不起作用:bash test04.sh -a'somestring' -b'somestring2' #either with or without quotes#getopt: invalid option -- 'b'#Try `getopt --help' for more information.#bash test04.sh --arga=somestring --argb=somestring2#getopt: unrecognized option '--argb=somestring2'#Try `getopt --help' for more information.你能帮我理解我的错误吗?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 508 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信