首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ksh getopts未知选项错误

ksh getopts未知选项错误
EN

Stack Overflow用户
提问于 2013-09-28 08:54:49
回答 1查看 1.8K关注 0票数 0

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

下面是我提供给getopts的用法字符串中的选项部分

代码语言:javascript
复制
#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代码块

代码语言:javascript
复制
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

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

代码语言:javascript
复制
$ ksh now.ksh -a
now.ksh: -a: unknown option
?
Usage: now.ksh [-wba] [-d day] [-m month] [-y year]
$
EN

回答 1

Stack Overflow用户

发布于 2013-09-29 06:39:40

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

我通过将[-]添加到用法字符串的前面来实现这一点:

代码语言:javascript
复制
USAGE+="[-][w:week?print the whole week]"

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19062116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档