好吧,我是一个getops和b一起玩的linux bash业余爱好者getop。我已经在几个论坛上阅读了有关该主题的几次对话,但似乎无法使我的代码正常工作。
这是一个使用的小脚本getopts,可从该论坛回收:
#!bin/bash
while getopts ":a:p:" opt; do
case $opt in
a) arg_1="$OPTARG"
;;
p) arg_2="$OPTARG"
;;
\?)
;;
esac
done
printf "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/bash
temp=`getopt -o a:b: -l arga:,argb:--"$@"`
eval set --"$temp"
while true ; do
case "$1" in
a|arga) firstArg="$OPTARG"
;;
b|argb) secondArg="$OPTARG"
;;
\?)
;;
esac
done
printf "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.
你能帮我理解我的错误吗?