开发者

bash Outputting invalid option

Say I have the following script to handle options:

while getopts dsf opts 2>/dev/null
do
    case $opts in
    d) echo "d";;
    s) echo "s";;
    \?) echo "Error: An invalid option [?] was entered.";
        exit 1;;
    esac
done

I want to replace the [?] with the invalid switch I entered. So if I enter

./myscript -z //output: Error: An invalid option [-z] was entered.

How would I catch that invalid switch? Using my $opts variable d开发者_如何学编程isplays a question mark.


From help getopts:

getopts reports errors in one of two ways.  If the first character
of OPTSTRING is a colon, getopts uses silent error reporting.  In
this mode, no error messages are printed.  If an invalid option is
seen, getopts places the option character found into OPTARG.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜