简体   繁体   English

如何正确使用测试命令?

[英]How do I play with test command properly?

[root@jiaoyou ~]# test 1 = 1 -a 0 = 1
[root@jiaoyou ~]# if [1 = 1 -a 0 = 1]then echo 1;else echo 2;fi
-bash: syntax error near unexpected token `else'

Why test doesn't give any output and the if statement fails? 为什么test没有给出任何输出并且if语句失败?

Can someone point out what's wrong here? 有人可以指出这里有什么问题吗?

UPDATE UPDATE

Can someone illustrate how to simulate complex expressions like 1=1 and (0=1 or 1=1) with [[ ? 有人可以举例说明如何用[[模拟复杂的表达式,例如1=1 and (0=1 or 1=1)

Return codes are not printed; 返回代码不打印; you must echo the value of $? 您必须回显$?的值$? in order to see it. 为了看到它。

[ is a command. [是命令。 Just as you don't write echoHelloworld , you can't write [1 ... . 正如您不编写echoHelloworld ,您也无法编写[1 ...

There are several equivalent ways of doing the test you're looking for. 有几种等效的测试方法。 First, essentially the way you're doing it, but with the syntax fixed (added required spaces around the parameters to the [ (aka test ) command, and a ; between ] and then ): 首先,基本上是您的操作方式,但语法已固定(在[ (aka test )命令的参数周围添加了必需的空格, then]之间加上了; ):

if [ 1 = 1 -a 0 = 1 ];then echo 1;else echo 2;fi

Here's a version with explicit grouping in the expression; 这是表达式中具有显式分组的版本; note that the parentheses have to be escaped because they're special characters in the shell, but in this case we want them passed to the [ command as arguments: 请注意,括号必须转义,因为它们是外壳程序中的特殊字符,但是在这种情况下,我们希望将它们作为参数传递给[命令:

if [ \( 1 = 1 \) -a \( 0 = 1 \) ];then echo 1;else echo 2;fi

Another version with explicit grouping, this time using two separate [ commands connected with bash's && operator (note that this can also connect other commands): 带有显式分组的另一个版本,这次使用两个单独的[与bash的&&运算符连接的命令(请注意,这也可以连接其他命令):

if [ 1 = 1 ] && [ 0 = 1 ];then echo 1;else echo 2;fi

And finally, a couple of examples using bash's [[ operator instead of the [ command; 最后,使用bash的[[运算符而不是[命令; note that [[ isn't a command, so its expression isn't parsed as command arguments, allowing it a more intuitive syntax (eg allowing && and || as operators, and parentheses and !<> comparisons without escaping): 请注意, [[不是命令,因此其表达式不会被解析为命令参数,从而使其具有更直观的语法(例如,允许&&||作为运算符,并且括号和!<>比较时无需转义):

if [[ 1 = 1 && 0 = 1 ]];then echo 1;else echo 2;fi
if [[ ( 1 = 1 ) && ( 0 = 1 ) ]];then echo 1;else echo 2;fi

Some more notes/warnings: the = operator being used in all of these examples does string comparisons rather than numeric comparison (ie [ 1 = 01 ] is false); 其他一些注意事项/警告:在所有这些示例中都使用=运算符进行字符串比较,而不是数字比较(即[ 1 = 01 ]为false); if you want numeric comparison use -eq instead (similarly, < and > are string comparisons, while -lt and -gt are numeric). 如果要进行数字比较,请改用-eq (类似, <>是字符串比较,而-lt-gt是数字比较)。 Also, if you're comparing strings that include spaces, '[' can mistake the string for part of the expression under some circumstances (esp. if you don't have the strings wrapped in double-quotes); 另外,如果您要比较包含空格的字符串,则在某些情况下,“ []可能会将字符串误认为表达式的一部分(尤其是如果字符串中没有用双引号引起来的话); as far as I know [[ never has this problem. 据我所知[[从来没有这个问题。 Overall, if you're using shells that support [[ , it's much easier to deal with than [ . 总的来说,如果您使用的支持[[外壳程序,则比[处理起来容易得多。

As @IgnacioVazquez-Abrams correctly states, test just sets the error code. 正如@ IgnacioVazquez-Abrams正确指出的那样, test仅设置错误代码。

That said, here's a nice bash-ism for quickly checking the error value: 就是说,这是一个很好的bash原理,用于快速检查错误值:

test [ expr ] && echo SUCCESS || echo FAIL

Due to how && and || 由于&&|| short circuit, that will output SUCCESS or FAIL depending on whether test returns 0 or non-0. 短路,将根据测试返回0还是非0来输出SUCCESSFAIL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM