Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

bash - 检查传递给Bash脚本的参数数量(Check number of arguments passed to a Bash script)

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

(如果不满足所需的参数计数,我希望我的Bash脚本能够打印错误消息。)

I tried the following code:

(我尝试了以下代码:)

#!/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

What am I doing wrong?

(我究竟做错了什么?)

  ask by Naftaly translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(就像任何其他简单命令一样, [ ... ]test需要在其参数之间留出空格。)

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

Or

(要么)

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

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.

(在Bash中,更喜欢使用[[ ]]因为它不会对其变量执行分词和路径名扩展,除非它是表达式的一部分,否则引用可能不是必需的。)

[[ $# -ne 1 ]]

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

(它还有一些其他功能,如不带引号的条件分组,模式匹配(与extglob扩展模式匹配)和正则表达式匹配。)

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> ]]

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:

(对于纯算术表达式,使用(( ))可能仍然会更好,但它们仍然可以在[[ ]]使用其算术运算符,如-eq-ne-lt-le-gt-ge将表达式放在单个字符串参数中:)

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.

(如果您需要将它与[[ ]]其他功能结合使用,这应该会有所帮助。)

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.

(这已经在ekangas评论中提出过,但有人编辑了这个答案,以-1作为返回值,所以我不妨正确地做到这一点。)

-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.

(-1虽然被Bash接受作为exit的参数没有明确记录,并且不适合用作常见建议。)

64 is also the most formal value since it's defined in sysexits.h with #define EX_USAGE 64 /* command line usage error */ .

(64也是最正式的值,因为它在sysexits.h使用#define EX_USAGE 64 /* command line usage error */ 。)

Most tools like ls also return 2 on invalid arguments.

(像ls这样的大多数工具也会在无效参数上返回2 。)

I also used to return 2 in my scripts but lately I no longer really cared, and simply used 1 in all errors.

(我也习惯在我的脚本中返回2 ,但最近我不再真正关心,只是在所有错误中使用1 。)

But let's just place 2 here since it's most common and probably not OS-specific.

(但是,让我们在这里放置2 ,因为它是最常见的,可能不是特定于操作系统的。)

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

References (参考)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...