【问题标题】:ksh getopts unknown option Errorksh getopts 未知选项错误
【发布时间】:2013-09-28 00:54:49
【问题描述】:

我正在使用 getopts 将选项传递给 ksh 脚本,但 getopts 无法识别这些选项。

这是我提供给 getopts 的使用字符串的选项部分

#OPTIONS
USAGE+="[w:week?print the whole week]"
USAGE+="[b:before?print the month up to and including the requested day]"
USAGE+="[a:after?print the month starting from the requested day to the end of the month or week]"
USAGE+="[d:day]#[day:=$(date "+%d"|sed 's/^0*//')?requested day]{[1-31]}"
USAGE+="[m:month]#[month:=$(date "+%m"|sed 's/^0*//')?month of requested day]{[1-12]}"
USAGE+="[y:year]#[year:=$(date "+%Y")?year of requested day.]"

这是我的 getopts 块

while getopts "$USAGE" optchar
do
    echo $optchar
    case $optchar
    in
            w)      boolWEEK=true;
                    ;;

            b)      boolBEFORE=true;
                    ;;
            a)      boolAFTER=true;
                    ;;
            d)      day=$OPTARG
                    ;;
            m)      month=$OPTARG
                    ;;
            y)      year=$OPTARG
                    ;;
            esac
done

这是使用选项运行脚本的输出

$ ksh now.ksh -a
now.ksh: -a: unknown option
?
Usage: now.ksh [-wba] [-d day] [-m month] [-y year]
$

【问题讨论】:

    标签: ksh getopts


    【解决方案1】:

    您似乎正在使用 ast getopts 语法。

    我通过在 USAGE 字符串的前面添加 [-] 来完成这项工作:

    USAGE+="[-][w:week?print the whole week]"
    

    也许需要[-] 来解决选项字符串中的任何歧义。

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 2014-10-13
      • 1970-01-01
      • 2017-05-20
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多