如何判断一个字符串是不是在bash shell脚本定义? [英] How to tell if a string is not defined in a bash shell script?

查看:167
本文介绍了如何判断一个字符串是不是在bash shell脚本定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要检查空字符串我会做

  [-z $ myStr中]

但如果我要检查是否变量都被定义?或者有没有在bash脚本没有区别?


解决方案

我觉得你是以后的答案是隐含的(如果没有说明)通过的的Vinko 回答,虽然它没有说出来简单。为了区分VAR是否被设置,但空或没有设置,你可以使用:

 如果[-z$ {VAR + XXX}];然后回声VAR是根本没有设置;科幻
如果[-z$ VAR]放;&安培; [$ {VAR + XXX}=XXX];然后回声VAR设置而空;科幻

您可能可以在第二行上的两个测试组合成一个与

 如果[-z$ VAR-a$ {VAR + XXX}=XXX];然后回声VAR设置而空;科幻

不过,如果你阅读的Autoconf的文档,你会发现,他们不建议用术语 -a '相结合,并使用单独的简单测试相结合做推荐与&放大器;&安培; 。我还没有遇到过一个系统,其中有一个问题;这并不意味着他们没有使用存在(但他们很可能是极为罕见的,这些天,即使他们不是在遥远的过去是很少见)。

您可以找到这些细节,以及其他相关的 shell参数扩展中的 测试 [ 指挥的有条件恩pressions 的在Bash的手册。


我最近通过电子邮件询问这个答案的问题:


  

您使用两次测试,我的理解,第二个好,但不是第一个。更多precisely我不明白变量扩展的需要。

 如果[-z$ {VAR + XXX}];然后回声VAR是根本没有设置;科幻


  
  

这是不是完成同样的?

 如果[-z$ {VAR}];然后回声VAR是根本没有设置;科幻


公平问题 - 答案是:不,你更简单的替代不会做同样的事情

假设我测试之前写:

  VAR =

您的测试会说:VAR没有设置在所有,但我会说(通过暗示,因为它呼应无)VAR设置,但其价值可能是空的。试试这个脚本:

 
未设置VAR
如果[-z$ {VAR + XXX}];然后回显JL:1 VAR是根本没有设置;科幻
如果[-z$ {VAR}];然后回显MP:1 VAR是根本没有设置;科幻
VAR =
如果[-z$ {VAR + XXX}];然后回显JL:2 VAR是根本没有设置;科幻
如果[-z$ {VAR}];然后回显MP:2 VAR是根本没有设置;科幻

输出是:

  JL:1 VAR是根本没有设置
MP:1 VAR是根本没有设置
MP:2 VAR是根本没有设置

在第二对测试中,变量设置,但它是设置为空值。这是在 $ {VAR =值} $的区别{VAR:=值} 符号做。同上,用于 $ {VAR值} $ {VAR:-value} $ {VAR +值} $ {VAR:+值}


由于吉利在他的答案,如果​​你运行上面的庆典设置-o nounset 选项,那么基本答案失败绑定变量。这是很容易纠正:

 如果[-z$ {VAR + XXX}];然后回声VAR是根本没有设置;科幻
如果[-z$ {} VAR-]&放大器;&安培; [$ {VAR + XXX}=XXX];然后回声VAR设置而空;科幻

或者你可以取消与设置-o nounset 选项设置+ U 设置-u 等同于设置-o nounset )。

If I want to check for the null string I would do

[ -z $mystr ]

but what if I want to check whether the variable has been defined at all? Or is there no distinction in bash scripting?

解决方案

I think the answer you are after is implied (if not stated) by Vinko's answer, though it is not spelled out simply. To distinguish whether VAR is set but empty or not set, you can use:

if [ -z "${VAR+xxx}" ]; then echo VAR is not set at all; fi
if [ -z "$VAR" ] && [ "${VAR+xxx}" = "xxx" ]; then echo VAR is set but empty; fi

You probably can combine the two tests on the second line into one with:

if [ -z "$VAR" -a "${VAR+xxx}" = "xxx" ]; then echo VAR is set but empty; fi

However, if you read the documentation for Autoconf, you'll find that they do not recommend combining terms with '-a' and do recommend using separate simple tests combined with &&. I've not encountered a system where there is a problem; that doesn't mean they didn't used to exist (but they are probably extremely rare these days, even if they weren't as rare in the distant past).

You can find the details of these, and other related shell parameter expansions, the test or [ command and conditional expressions in the Bash manual.


I was recently asked by email about this answer with the question:

You use two tests, and I understand the second one well, but not the first one. More precisely I don't understand the need for variable expansion

if [ -z "${VAR+xxx}" ]; then echo VAR is not set at all; fi

Wouldn't this accomplish the same?

if [ -z "${VAR}" ]; then echo VAR is not set at all; fi

Fair question - the answer is 'No, your simpler alternative does not do the same thing'.

Suppose I write this before your test:

VAR=

Your test will say "VAR is not set at all", but mine will say (by implication because it echoes nothing) "VAR is set but its value might be empty". Try this script:

(
unset VAR
if [ -z "${VAR+xxx}" ]; then echo JL:1 VAR is not set at all; fi
if [ -z "${VAR}" ];     then echo MP:1 VAR is not set at all; fi
VAR=
if [ -z "${VAR+xxx}" ]; then echo JL:2 VAR is not set at all; fi
if [ -z "${VAR}" ];     then echo MP:2 VAR is not set at all; fi
)

The output is:

JL:1 VAR is not set at all
MP:1 VAR is not set at all
MP:2 VAR is not set at all

In the second pair of tests, the variable is set, but it is set to the empty value. This is the distinction that the ${VAR=value} and ${VAR:=value} notations make. Ditto for ${VAR-value} and ${VAR:-value}, and ${VAR+value} and ${VAR:+value}, and so on.


As Gili points out in his answer, if you run bash with the set -o nounset option, then the basic answer above fails with unbound variable. It is easily remedied:

if [ -z "${VAR+xxx}" ]; then echo VAR is not set at all; fi
if [ -z "${VAR-}" ] && [ "${VAR+xxx}" = "xxx" ]; then echo VAR is set but empty; fi

Or you could cancel the set -o nounset option with set +u (set -u being equivalent to set -o nounset).

这篇关于如何判断一个字符串是不是在bash shell脚本定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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