本文介绍了用X“"测试空字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用-z在Bash中测试一个空字符串,如下所示:

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

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

但是我看到很多代码如下:

but I see a lot of code written like:

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

该方法更便于移植吗?仅仅是-z之前的历史遗留物吗?是否适用于POSIX shell(即使我已经在针对bash的脚本中看到了它)?准备好参加我的历史记录/便携式课程.

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变量是否为空?相同,但没有人提出为什么的解释,您会看到带有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.

推荐答案

从根本上讲,因为在很久以前,test的行为更加复杂,而且在不同系统上的定义不统一(因此必须编写可移植代码仔细避免使用非便携式构造.

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

尤其是,在test是内置的shell之前,它是一个单独的可执行文件(请注意,MacOS X仍然具有/bin/test/bin/[作为可执行文件).在这种情况下,请写:

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 ]

$variable为空时,将通过其别名[并使用3个参数来调用测试程序:

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

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

因为该变量为空,所以没有任何可扩展的内容.因此,编写代码的安全方法是:

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

if [ -z "$variable" ]

这可靠地工作,将4个参数传递给test可执行文件.可以肯定的是,该测试程序几十年来一直是大多数外壳程序的内置程序,但是旧设备很难被使用,因此更早以前学到的良好实践也是如此.

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前缀解决的另一个问题是,如果变量包含前导破折号,或者包含等号或其他比较器,则会发生什么情况.考虑一下(这不是一个非常好的例子):

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代码仍然比现代shell实际使用的双引号更加丰富,并且使用X前缀表示法用来确保事情不会被误解.

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“"测试空字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:59