在shell脚本中,对于命令行参数的获取通常使用getopts函数。在getopts函数中,只支持单破折号选项和双破折号选项。如果使用其他字符作为选项前缀会出现“bash: get only double dash or single dash options”错误。
示例代码如下:
while getopts ":ab:c" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
b)
echo "-b was triggered! $OPTARG" >&2
;;
c)
echo "-c was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
在上述示例代码中,变量$opt表示当前选项,$OPTARG表示当前选项的值。其中,选项可以使用单破折号和双破折号形式。如果getopts函数中的选项前缀不是单破折号或双破折号,就会出现上述错误。