"test

编程入门 行业动态 更新时间:2024-10-19 12:43:47
本文介绍了"test -v $ TESTENV"即使定义了TESTENV,也始终为假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在Fedora 27下运行Bash 4.4.19,我有一个简单的脚本:

I'm running Bash 4.4.19, under Fedora 27, and I have a simple script:

#!/bin/bash TESTENV="Hello" echo "$TESTENV" if [ -v $TESTENV ]; then echo "Yup" fi echo "Done"

运行此命令时,它会打印:

When I run this, it prints:

Hello ./myscript: line 3: [: Hello: binary operator expected Done

因此,我在第3行添加了多余的括号,现在看起来像这样:

So, I add the extra bracket to line 3, which now looks like:

if [[ -v $TESTENV ]]; then

但是会产生:

Hello Done

有什么用?我希望以上两者之一都能看到是".我已经尝试将"$ TESTENV" 用引号引起来,并且还尝试了 -z 运算符-但其行为是相同的.

What gives? I expected to see "Yup" for one/both of the above. I've tried wrapping "$TESTENV" in quotes, and I've also tried the -z operator instead - but the behavior is the same.

推荐答案

对于 $ ,您使用的是变量的 value ,而不是变量名本身.您可能是说:

With $, you are using the value of the variable, not the variable name itself. You probably meant:

if [[ -v TESTENV ]]; then echo "Yup" fi

如果您定义了名为 Hello 的变量(例如 Hello = hi ),则 if 条件将会成功,并且您可能没有注意到根本没有问题:)

If you defined variable named Hello (such as Hello=hi) the if condition would have succeeded and you might not have noticed the problem at all :)

更多推荐

"test

本文发布于:2023-11-13 00:23:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:quot   test

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!