bash。为什么“ while”条件不正确的声明有效吗?

发布于 2025-01-22 01:54:04 字数 104 浏览 2 评论 0原文

while ['qwe'='rty']
do
echo yes
done

这种情况显然是不正确的,但是终端是无限期地打印“是”。 为什么会发生?

while ['qwe'='rty']
do
echo yes
done

The condition is obviously incorrect, yet terminal is printing "yes" indefinitely.
Why is that happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2025-01-29 01:54:04

这种情况显然不正确...

不,它确实不是不是 (1)

在条件中需要空格(包括包括“命令”空间,将[ and ]字符与条件分开(2) ))对于bash将条件解释为三部分测试。

您提供的内容(假设上面描述的缺失命令空间是简单的错别字)被视为一部分测试,只要它不是空字符串,它是正确的。

您可以看到下面的区别:

pax> [ 'abc' = 'xyz' ] && echo yes

pax> [ 'abc' = 'xyz' ] || echo no
no

pax> [ 'abc'='xyz' ] && echo yes
yes

bash man-page的相关部分具有此内容(请注意 spaces 在等式检查中,它们不仅是为在人页本身):

字符串

-n String

true如果字符串的长度不零。

string1 == String2

String1 = String2

true如果字符串相等。 =应与test posix符合命令一起使用。

换句话说,您的应该作为,而语句是:

while [ 'qwe' = 'rty' ]

(1),从技术上讲,它是 is 不正确,但不是从您的意思(错误)中。相反,这是不正确的,因为它不是您的表达式 think 它是:-)


(2)没有这些命令空间,您会看到沿着该命令的错误线的行:

pax> ['abc' = 'xyz'] && echo yes
[abc: command not found

pax> ['abc'='xyz'] && echo yes
[abc=xyz]: command not found

当然,除非您实际上 可执行的目标称为[abc = xyz]在这种情况下,它将被执行,返回值将决定哪种操作是拍摄。这是一个不可能具有可执行性的目标,但它是可能的:

pax> echo true > '[abc=xyx]' ; PATH=$PATH:.

pax> ['abc'='xyx'] && echo yes
yes

The condition is obviously incorrect ...

No, it really isn't(1).

Spaces are required in the conditional (including "command" spaces separating the [ and ] characters from the condition by the way(2)) for bash to interpret the condition as a three-part test.

What you have provided (assuming your missing command spaces described above are a simple typo) is treated as a one-part test which is true so long as it is not an empty string.

You can see the difference below:

pax> [ 'abc' = 'xyz' ] && echo yes

pax> [ 'abc' = 'xyz' ] || echo no
no

pax> [ 'abc'='xyz' ] && echo yes
yes

The relevant section of the bash man-page has this (note the spaces in the equality checks, they're not just for separating tokens in the man-page itself):

string
-n string

True if the length of string is non-zero.

string1 == string2
string1 = string2

True if the strings are equal. = should be used with the test command for POSIX conformance.

In other words, what you should have as the while statement is:

while [ 'qwe' = 'rty' ]

(1) Well, technically, it is incorrect, but not in the sense you mean (false). Instead, it's incorrect in the sense that it's not the expression you think it is :-)


(2) Without those command spaces, you'll see a syntax error along the lines of:

pax> ['abc' = 'xyz'] && echo yes
[abc: command not found

pax> ['abc'='xyz'] && echo yes
[abc=xyz]: command not found

Unless, of course, you actually have an executable target called [abc=xyz] in which case it will be executed and the return value will decide what action is taken. This is an unlikely executable target to have but it is possible:

pax> echo true > '[abc=xyx]' ; PATH=$PATH:.

pax> ['abc'='xyx'] && echo yes
yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文