检查传递给Bash脚本的参数数量 [英] Check number of arguments passed to a Bash script

查看:87
本文介绍了检查传递给Bash脚本的参数数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不满足所需的参数计数,我希望Bash脚本打印一条错误消息.

I would like my Bash script to print an error message if the required argument count is not met.

我尝试了以下代码:

#!/bin/bash
echo Script name: $0
echo $# arguments 
if [$# -ne 1]; 
    then echo "illegal number of parameters"
fi

由于某些未知原因,我遇到了以下错误:

For some unknown reason I've got the following error:

test: line 4: [2: command not found

我在做什么错了?

推荐答案

与其他任何简单命令一样,[ ... ]test的参数之间也需要空格.

Just like any other simple command, [ ... ] or test requires spaces between its arguments.

if [ "$#" -ne 1 ]; then
    echo "Illegal number of parameters"
fi

if test "$#" -ne 1; then
    echo "Illegal number of parameters"
fi

建议

在Bash中使用时,最好使用[[ ]],因为它不会对变量进行分词和路径名扩展,除非引用是表达式的一部分,否则可能不需要引用.

Suggestions

When in Bash, prefer using [[ ]] instead as it doesn't do word splitting and pathname expansion to its variables that quoting may not be necessary unless it's part of an expression.

[[ $# -ne 1 ]]

它还具有其他一些功能,例如无引号的条件分组,模式匹配(使用extglob进行扩展的模式匹配)和正则表达式匹配.

It also has some other features like unquoted condition grouping, pattern matching (extended pattern matching with extglob) and regex matching.

以下示例检查参数是否有效.它允许一个或两个参数.

The following example checks if arguments are valid. It allows a single argument or two.

[[ ($# -eq 1 || ($# -eq 2 && $2 == <glob pattern>)) && $1 =~ <regex pattern> ]]

对于纯算术表达式,在某些地方使用(( ))可能会更好,但是在[[ ]]中,通过-eq-ne-lt-le-gt-ge通过将表达式作为单个字符串参数放置:

For pure arithmetic expressions, using (( )) to some may still be better, but they are still possible in [[ ]] with its arithmetic operators like -eq, -ne, -lt, -le, -gt, or -ge by placing the expression as a single string argument:

A=1
[[ 'A + 1' -eq 2 ]] && echo true  ## Prints true.

如果您还需要将其与[[ ]]的其他功能结合使用,那将会很有帮助.

That should be helpful if you would need to combine it with other features of [[ ]] as well.

请注意,[[ ]](( ))是与ifcasewhilefor具有相同解析级别的关键字.

Take note that [[ ]] and (( )) are keywords which have same level of parsing as if, case, while, and for.

还建议使用 Dave ,将错误消息更好地发送到stderr,以便在重定向stdout时不将其包含在内:

Also as Dave suggested, error messages are better sent to stderr so they don't get included when stdout is redirected:

echo "Illegal number of parameters" >&2

退出脚本

在将无效参数传递给脚本时退出脚本也是合乎逻辑的. 评论中已经建议了这一点.由 ekangas 编写,但有人编辑了此答案,以-1作为返回值,所以我也可以这样做是的.

Exiting the script

It's also logical to make the script exit when invalid parameters are passed to it. This has already been suggested in the comments by ekangas but someone edited this answer to have it with -1 as the returned value, so I might as well do it right.

-1被Bash接受为exit的参数,但没有明确记录,也没有权利用作常见建议. 64也是最正式的值,因为它是在sysexits.h中用#define EX_USAGE 64 /* command line usage error */定义的.大多数工具,例如ls也会在无效参数上返回2.我也曾经在脚本中返回2,但是最近我不再真正关心它了,只是在所有错误中都使用了1.但是,让我们将2放在这里,因为它是最常见的,并且可能不是特定于操作系统的.

-1 though accepted by Bash as an argument to exit is not explicitly documented and is not right to be used as a common suggestion. 64 is also the most formal value since it's defined in sysexits.h with #define EX_USAGE 64 /* command line usage error */. Most tools like ls also return 2 on invalid arguments. I also used to return 2 in my scripts but lately I no longer really cared, and simply used 1 in all errors. But let's just place 2 here since it's most common and probably not OS-specific.

if [[ $# -ne 1 ]]; then
    echo "Illegal number of parameters"
    exit 2
fi

参考文献

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆