"对于带X&QUOT空字符串击试验; [英] Bash test for empty string with X""

查看:96
本文介绍了"对于带X&QUOT空字符串击试验;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以测试在击空字符串 -z 像这样:

I know I can test for an empty string in Bash with -z like so:

if [[ -z $myvar ]]; then do_stuff; fi

但我看到很多code的这样写:

but I see a lot of code written like:

if [[ X"" = X"$myvar" ]]; then do_stuff; fi

时的那种方法更便携?难道仅仅是历史的克鲁夫特从 -z 的前几天?难道是对POSIX壳(即使我已经看到了它在脚本中使用的目标庆典)?准备好我的历史/便携教训。

Is that method more portable? Is it just historical cruft from before the days of -z? Is it for POSIX shells (even though I've seen it used in scripts targeting bash)? Ready for my history/portability lesson.

同样的问题被要求在服务器故障的如何确定一个bash变量是空的?但没有人提供了一个解释到的为什么的你看code与 X的东西。

The same question was asked on Server Fault as How to determine if a bash variable is empty? but no one offered an explanation as to why you see code with the X"" stuff.

推荐答案

从根本上说,是因为时代现在早已过去,行为测试更复杂,不能一概而定在不同系统(所以便携code必须浓墨重彩地避免不可移植的结构)。

Fundamentally, because in times now long past, the behaviour of test was more complex and not uniformly defined across different systems (so portable code had to be written carefully to avoid non-portable constructs).

在特定的,之前测试是内置了一个外壳,它是一个独立的可执行文件(请注意,MacOS X系统仍然有 /斌/测试 /斌/ [为可执行文件)。如果是这样的话,写:

In particular, before test was a shell built-in, it was a separate executable (and note that MacOS X still has /bin/test and /bin/[ as executables). When that was the case, writing:

if [ -z $variable ]

$变量是空的,将通过它的别名调用测试程序 [有3个参数:

when $variable was empty would invoke the test program via its alias [ with 3 arguments:

argv[0] = "["
argv[1] = "-z"
argv[2] = "]"

因为变量是空的,所以没有什么扩大。所以,写code的安全方法是:

because the variable was empty so there was nothing to expand. So, the safe way of writing the code was:

if [ -z "$variable" ]

这工作可靠,传递4个参数的测试可执行文件。诚然,测试程序已经内置在大多数弹了几十年,但旧设备模具辛苦,所以做的好做法,学会甚至更长的时间之前。

This works reliably, passing 4 arguments to the test executable. Granted, the test program has been a built-in to most shells for decades, but old equipment dies hard, and so do good practices learned even longer ago.

由X preFIX解决的另一个问题是发生了什么事,如果变量包括破折号,或者包含等于或其他比较器。考虑(未desparately很好的例子):

The other problem resolved by the X prefix was what happened if variables include leading dashes, or contain equals or other comparators. Consider (a not desparately good example):

x="-z"
if [ $x -eq 0 ]

是一个流浪(错误)的说法,或者非数字的第一个参数的数值相等测试一个空字符串的测试?不同的系统提供不同的答案POSIX标准的行为之前,大约1990年。所以,处理这个安全的方法是:

Is that an empty string test with a stray (erroneous) argument, or a numeric equality test with a non-numeric first argument? Different systems provided different answers before POSIX standardized the behaviour, circa 1990. So, the safe way of dealing with this was:

if [ "X$x" = "X0" ]

或(通常少,以我的经验,而是完全等价):

or (less usually, in my experience, but completely equivalently):

if [ X"$x" = X"0" ]

这是所有的边缘情况是这样,捆绑的可能性,测试是一个单独的可执行文件,这意味着移植的shell code仍然使用双引号更丰富地比现代炮弹实际需要和X - preFIX符号是用来保证事情不能得到misinter preTED

It was all the edge cases like this, tied up with the possibility that the test was a separate executable, that means that portable shell code still uses double quotes more copiously than the modern shells actually require, and the X-prefix notation was used to ensure that things could not get misinterpreted.

这篇关于"对于带X&QUOT空字符串击试验;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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