bash 脚本的以下行有什么作用?

发布于 2024-08-30 07:20:56 字数 284 浏览 4 评论 0原文

通常在 Windows 上工作,但尝试在我的 Mac 上设置 RabbitMQ。有人可以让我知道下面这一行的作用吗?

[ "x" = "x$RABBITMQ_NODE_IP_ADDRESS" ] && [ "x" != "x$NODE_IP_ADDRESS" ] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

具体来说,我对 [ "x" = "x$RAB..."] 语法感到好奇。

Usually work in Windows, but trying to setup RabbitMQ on my Mac. Can someone let me know what the line below does?

[ "x" = "x$RABBITMQ_NODE_IP_ADDRESS" ] && [ "x" != "x$NODE_IP_ADDRESS" ] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

Specifically, I'm curious about the [ "x" = "x$RAB..."] syntax.

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

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

发布评论

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

评论(4

筱果果 2024-09-06 07:20:56

如果 RABBITMQ_NODE_IP_ADDRESS 变量为空/不存在,它将计算为 "x" = "x" ,这是正确的。

所以它基本上是说,如果未设置 RABBITMQ_NODE_IP_ADDRESS 并且设置了 NODE_IP_ADDRESS,则设置 RABBITMQ_NODE_IP_ADDRESS=NODE_IP_ADDRESS

If the RABBITMQ_NODE_IP_ADDRESS variable is empty/doesn't exist, it'll evaluate as "x" = "x" , which is true.

So it basically says, if RABBITMQ_NODE_IP_ADDRESS isn't set and NODE_IP_ADDRESS is set, set RABBITMQ_NODE_IP_ADDRESS=NODE_IP_ADDRESS

烧了回忆取暖 2024-09-06 07:20:56

如果变量为空或未设置,则使用“x”(有点迷信*)来防止错误。大多数时候,报价会帮你解决这个问题。通过将文字放在第一位,然后将变量放在第二位,可以消除变量包含以破折号开头的字符串的情况下的错误,因为 test (又名 [)会认为它是一个操作员。在您的示例中,最好使用 -z-n 运算符来测试变量是否为空(null 或未设置)或不为空,分别。

POSIX shell,例如 Bourne(也适用于 Bash):

[ -z $RABBITMQ_NODE_IP_ADDRESS ] && [ -n $NODE_IP_ADDRESS" ] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

Bash(以及 kshzsh):

[[ -z $RABBITMQ_NODE_IP_ADDRESS && -n $NODE_IP_ADDRESS" ]] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

* 可能有一些 shell 需要“x”,但是有些人这样做“因为一直都是这样做的”。

The "x" is used (somewhat superstitiously*) to prevent errors if the variable is null or unset. Most of the time the quotes take care of that for you. By putting the literal first and the variable second you eliminate errors in cases where the variable contains a string that starts with a dash, since test (aka [) would think it is an operator. In the case of your example, it would be preferable to use the -z and -n operators that test whether a variable is empty (null or unset) or not empty, respectively.

POSIX shells, such as Bourne (works in Bash, too):

[ -z $RABBITMQ_NODE_IP_ADDRESS ] && [ -n $NODE_IP_ADDRESS" ] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

Bash (and ksh and zsh):

[[ -z $RABBITMQ_NODE_IP_ADDRESS && -n $NODE_IP_ADDRESS" ]] && RABBITMQ_NODE_IP_ADDRESS=${NODE_IP_ADDRESS}

* There may be some shells that need the "x", but some people do that "because it's always been done that way".

稀香 2024-09-06 07:20:56

“x”并不总是迷信,即使在我相对较新的 bash (4.0.33) 中也是如此。

让我们把运算放在括号之间。空变量没问题:

$ a=""
$ b=""
$ if [ '(' "$a" = "$b" ')' ]; then echo both_equal; fi
both_equal

但是 ! 运算符就不行:

$ a='!'
$ if [ '(' "$a" = "$b" ')' ]; then echo both_equal; fi
bash: [: `)' expected, found

如果我们写 "x$a" = "x$b" 而不是 <,这不是问题代码>“$a”=“$b”。

The "x" is not always superstitious, even in my relatively new bash (4.0.33).

Let's put the operation between parens. Empty variables are fine:

$ a=""
$ b=""
$ if [ '(' "$a" = "$b" ')' ]; then echo both_equal; fi
both_equal

But the ! operator for instance is not:

$ a='!'
$ if [ '(' "$a" = "$b" ')' ]; then echo both_equal; fi
bash: [: `)' expected, found

This is not a problem if we write "x$a" = "x$b" instead of "$a" = "$b".

双手揣兜 2024-09-06 07:20:56

括号[是一个test运算符,您可以将其视为if语句。这是检查 shell 变量 RABBITMQ_NODE_IP_ADDRESS 是否为空。不幸的是,如果您尝试与空字符串 "" 进行比较,shell 在执行测试之前会将其消除,并且您的二进制比较运算符仅获得一个(或可能是零)操作数。为了防止该错误,通常的做法是在 = 的每一侧连接一个“x”。因此,而不是

[ "" = "<variable>" ]

成为
[ = 值 ]
并产生错误,

[ "X" = "X<variable>" ]

变成
[ X = Xvalue ]

并且比较可以继续

The bracket [ is a test operator, which you can think of as an if statement. This is checking to see if the shell variable RABBITMQ_NODE_IP_ADDRESS is empty. Unfortunately, if you try to compare to an empty string "", the shell eliminates it before it does the test and your binary comparison operator only gets one (or maybe zero) operands. To prevent that error, it is a common practice to concatenate an "x" on each side of the =. Thus, instead of

[ "" = "<variable>" ]

becoming
[ = value ]
and yielding an error,

[ "X" = "X<variable>" ]

becomes
[ X = Xvalue ]

and the comparison may continue

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