Bash 如何检测传递给脚本的缺少的强制参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19842538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:32:18  来源:igfitidea点击:

Bash how to detect missing mandatory arguments passed to a script

bash

提问by Alain

I have a Bash script and I need to pass a certain number of arguments to make it work.

./upload.sh $ARG1 $ARG2 $ARG3

Let's say that the 2 mandatory fields are ARG1 and ARG2, and
ARG1 and 3 are not empty.

我有一个 Bash 脚本,我需要传递一定数量的参数才能使其工作。

./upload.sh $ARG1 $ARG2 $ARG3

假设 2 个必填字段是 ARG1 和 ARG2,并且
ARG1 和 3 不为空。

I think the script will run and think that it has the 2 mandatory arguments, Is there a way to detect that ARG2 is missing/empty? I need to return exit 1 and not exit 0.

我认为脚本会运行并认为它有 2 个强制参数,有没有办法检测 ARG2 丢失/空?我需要返回 exit 1 而不是 exit 0。

Here's a bit of the script

这里有一些脚本

RESOURCE=
CONTAINER=
APP_NAME=

if [[ -z $RESOURCE || -z $CONTAINER ]];
then
    echo `date`" - Missing mandatory arguments: resource and container. "
    echo `date`" - Usage: ./upload.sh  [resource] [container] [appname] . "
    exit 1
fi

Thanks in advance,

提前致谢,

Alain

阿兰

回答by user3270760

I always use a little function to check all the arguments:

我总是使用一个小函数来检查所有参数:

process_arguments() {
    while [ -n "" ]
    do
        case  in
            -h|--help) echo "some usage details"; exit 1;;
            -x) do_something; shift; break;;
            -y) do_something_else; shift; break;;
            *) echo "some usage details"; exit 1;;
        esac
        echo ; shift
    done
}

This way if you miss anything, the proper usage details will be displayed the script will exit. Otherwise, you can enter the args in any order. When your script starts, just call

这样,如果您错过任何内容,将显示正确的使用详细信息,脚本将退出。否则,您可以按任何顺序输入参数。当您的脚本启动时,只需调用

process_arguments "$@"

and you should be all set.

你应该准备好了。

回答by devnull

Is there a way to detect that ARG2 is missing/empty?

有没有办法检测 ARG2 缺失/空?

NO. The way you're passing the arguments, those would be interpreted as arguments 1 and 2.

不。您传递参数的方式,这些将被解释为参数 1 和 2。

You could instead say:

你可以说:

./upload.sh "$ARG1" "$ARG2" "$ARG3"

in order to make bash interpret the arguments correctly whether those are empty or not.

为了使 bash 正确解释参数,无论这些参数是否为空。



Example:

例子:

$ cat file
[ -z  ] && echo "Arg 1 missing"
[ -z  ] && echo "Arg 2 missing"
[ -z  ] && echo "Arg 3 missing"
$ bash file "$HOME" "$FOOBAR" "$USER"
Arg 2 missing

(The variable FOOBARwas undefined in the above example.)

FOOBAR在上面的例子中变量是未定义的。)

回答by voidlogic

Args as you are using them are positional so is you only provided 1 and 3, but 1 and 2 are require, you just accidentally provided a value for arg 3 as arg 2.

使用它们时的 Args 是位置性的,因此您是否只提供了 1 和 3,但是 1 和 2 是必需的,您只是不小心将 arg 3 的值提供为 arg 2。

You could require that your optional arguments start with a dash -, and if you find either $1 or $2 start with a dash you know you are missing a required. You of course would have to remove the dash from $3.

你可以要求你的可选参数以破折号 - 开头,如果你发现 $1 或 $2 以破折号开头,你就知道你缺少一个必需的。您当然必须从 $3 中删除破折号。

A better way to handle args is to use getopts. Here are a couple of resources to get you started:

处理 args 的更好方法是使用getopts。以下是一些可帮助您入门的资源:

This will allow you to handle args in a way similar to standard commandline utils. If you write commandline utils in C you have probably used the C library version of getops before.

这将允许您以类似于标准命令行实用程序的方式处理 args。如果您用 C 编写命令行实用程序,那么您之前可能使用过 getops 的 C 库版本。

回答by damienfrancois

I think the script will run and think that it has the 2 mandatory arguments, Is there a way to detect that ARG2 is missing/empty?

我认为脚本会运行并认为它有 2 个强制参数,有没有办法检测 ARG2 丢失/空?

No unless ARG2either

没有,除非ARG2要么

  • has some identifiable structure such as a URL, an email, or a bank account can have, or
  • is the named of an object whose existence can be tested like a file, an entry in a file, etc.
  • 具有一些可识别的结构,例如 URL、电子邮件或银行帐户可以具有,或
  • 是可以像文件、文件中的条目等一样测试其存在的对象的名称。

If any of the above holds, you can write a piece of code that will determine whether ARG2really corresponds to a CONTAINERor is most likely a APP_NAME

如果上述任何一项成立,您可以编写一段代码来确定是否ARG2真的对应于 aCONTAINER或最有可能是 aAPP_NAME

If not, you will need either to

如果没有,您将需要

  • make the third argument mandatory
  • drop the use of positional arguments in favour of named arguments e.g. ./upload.sh --resource blah --container blahblah --appname blablahblah, with the handy getopts.
  • 强制第三个参数
  • 放弃使用位置参数,转而使用命名参数,例如./upload.sh --resource blah --container blahblah --appname blablahblah,使用方便的getopts.