bash 使用“set -o nounset”测试是否在bash中设置了变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7832080/
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-09 21:07:48  来源:igfitidea点击:

Test if a variable is set in bash when using "set -o nounset"

bashshellunix

提问by vinodkone

The following code exits with a unbound variable error. How to fix this, while still using the set -onounset option?

以下代码以未绑定变量错误退出。如何解决这个问题,同时仍然使用set -onounset 选项?

#!/bin/bash

set -o nounset

if [ ! -z ${WHATEVER} ];
 then echo "yo"
fi

echo "whatever"

采纳答案by Angelom

#!/bin/bash

set -o nounset


VALUE=${WHATEVER:-}

if [ ! -z ${VALUE} ];
 then echo "yo"
fi

echo "whatever"

In this case, VALUEends up being an empty string if WHATEVERis not set. We're using the {parameter:-word}expansion, which you can look up in man bashunder "Parameter Expansion".

在这种情况下,VALUE如果WHATEVER未设置,则最终为空字符串。我们正在使用{parameter:-word}扩展,您可以man bash在“参数扩展”下查找。

回答by l0b0

You need to quote the variables if you want to get the result you expect:

如果你想得到你期望的结果,你需要引用变量:

check() {
    if [ -n "${WHATEVER-}" ]
    then
        echo 'not empty'
    elif [ "${WHATEVER+defined}" = defined ]
    then
        echo 'empty but defined'
    else
        echo 'unset'
    fi
}

Test:

测试:

$ unset WHATEVER
$ check
unset
$ WHATEVER=
$ check
empty but defined
$ WHATEVER='   '
$ check
not empty

回答by NublaII

How about a oneliner?

单线呢?

[ -z "${VAR:-}" ] && echo "VAR is not set or is empty" || echo "VAR is set to $VAR"

-zchecks both for empty or unset variable

-z检查空变量或未设置变量

回答by Acumenus

Assumptions:

假设:

$ echo $SHELL
/bin/bash
$ /bin/bash --version | head -1
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
$ set -o nounset

If you want a non-interactive script to print an error and exit if a variable is null or not set:

如果您希望非交互式脚本打印错误并在变量为空或未设置时退出:

$ [[ "${HOME:?}" ]]

$ [[ "${IAMUNBOUND:?}" ]]
bash: IAMUNBOUND: parameter null or not set

$ IAMNULL=""
$ [[ "${IAMNULL:?}" ]]
bash: IAMNULL: parameter null or not set

If you don't want the script to exit:

如果您不希望脚本退出:

$ [[ "${HOME:-}" ]] || echo "Parameter null or not set."

$ [[ "${IAMUNBOUND:-}" ]] || echo "Parameter null or not set."
Parameter null or not set.

$ IAMNULL=""
$ [[ "${IAMUNNULL:-}" ]] || echo "Parameter null or not set."
Parameter null or not set.

You can even use [and ]instead of [[and ]]above, but the latter is preferable in Bash.

您甚至可以使用[and]代替[[and]]上面,但后者在 Bash 中更可取。

Note what the colon does above. From the docs:

注意上面冒号的作用。从文档

Put another way, if the colon is included, the operator tests for both parameter's existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

换句话说,如果包含冒号,则运算符会测试两个参数是否存在,并且其值不为空;如果省略冒号,则运算符仅测试是否存在。

There is apparently no need for -nor -z.

显然不需要-nor -z

In summary, I may typically just use [[ "${VAR:?}" ]].Per the examples, this prints an error and exits if a variable is null or not set.

总之,我通常可能只使用[[ "${VAR:?}" ]]. 根据示例,如果变量为空或未设置,则会打印错误并退出。

回答by Ale? Friedl

You can use

您可以使用

if [[ ${WHATEVER:+$WHATEVER} ]]; then

but

if [[ "${WHATEVER:+isset}" == "isset" ]]; then

might be more readable.

可能更具可读性。

回答by Max Bileschi

While this isn't exactlythe use case asked for above, I've found that if you want to use nounset(or -u) the default behavior is the one you want: to exit nonzero with a descriptive message.

虽然这不是正是用例要求上面,我发现,如果你想使用nounset(或-u)的默认行为是你想要的:退出非零一个描述性的消息。

It took me long enough to realize this that I figured it was worth posting as a solution.

我花了很长时间才意识到这一点,我认为值得将其作为解决方案发布。

If all you want is to echo something else when exiting, or do some cleanup, you can use a trap.

如果您只想在退出时回显其他内容或进行一些清理,则可以使用陷阱。

The :-operator is probably what you want otherwise.

:-否则,操作员可能就是您想要的。