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

查看:28
本文介绍了检查传递给 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.

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

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

退出脚本

在传递无效参数时让脚本退出也是合乎逻辑的.这已在评论中提出来自 ekanas 但有人编辑了这个答案,以 -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/* 命令行使用错误 */ 定义.像 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" >&2
    exit 2
fi

参考资料

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