我想将选项作为参数传递。例如。:

mycommand -a 1 -t '-q -w 111'

脚本无法识别引号中的字符串。也就是说,它只得到绳子的一部分。
getopts的工作原理相同-它只看到-q
对于自定义getopts,我使用类似的脚本(示例):
while :
do
    case $1 in
        -h | --help | -\?)
            # Show some help
            ;;
        -p | --project)
            PROJECT="$2"
            shift 2
            ;;
        -*)
            printf >&2 'WARN: Unknown option (ignored): %s\n' "$1"
            shift
            ;;
        *)  # no more options. Stop while loop
            break
            ;;
        --) # End of all options
        echo "End of all options"
            shift
            break
            ;;
    esac
done

最佳答案

也许我误解了这个问题,但是getopts似乎对我有用:

while getopts a:t: arg
do
    case $arg in
        a)  echo "option a, argument <$OPTARG>"
            ;;
        t)  echo "option t, argument <$OPTARG>"
            ;;
    esac
done

运行:
bash gash.sh -a 1 -t '-q -w 111'
option a, argument <1>
option t, argument <-q -w 111>

这不是你想要的吗?也许你错过了带参数选项后面的:

07-28 03:04
查看更多