ksh getopts未知选项错误

时间:2013-09-28 00:54:49

标签: ksh getopts

我使用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]
$

1 个答案:

答案 0 :(得分:0)

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

我通过将[-]添加到USAGE字符串的前面来实现此功能:

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

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