如何判断在Bash Shell脚本中是否未定义字符串 [英] How to tell if a string is not defined in a Bash shell script

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

问题描述

如果我想检查空字符串,我会做

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

[ -z $mystr ]

但是如果我想检查变量是否已经定义怎么办?还是Bash脚本没有区别?

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

推荐答案

我认为 answer ,尽管并非简单说明.要区分VAR是已设置还是空的,可以使用:

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

但是,如果您阅读有关Autoconf的文档,您会发现他们不建议将术语与'-a'组合使用,并且建议将单独的简单测试与&&组合使用.我没有遇到问题的系统.那并不意味着它们就不曾存在过(但是,即使它们在遥远的过去并不稀罕,但如今它们可能极为稀有).

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

您可以找到其中的详细信息以及其他相关的 shell参数扩展 test[ 命令和条件表达式在Bash手册中.

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=

您的测试将说根本没有设置VAR",但是我的孩子会说(暗示,因为它没有回显任何内容)"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
)

输出为:

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

在第二对测试中,设置了变量,但将其设置为空值.这是${VAR=value}${VAR:=value}表示法的区别.与${VAR-value}${VAR:-value}以及${VAR+value}${VAR:+value}相同,依此类推.

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.

吉莉在他的 answer ,如果您使用set -o nounset选项运行bash,则上面的基本答案将失败,并显示unbound variable.很容易补救:

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

或者您可以使用set +u取消set -o nounset选项(set -u等同于set -o nounset).

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天全站免登陆