shell脚本测试 [英] Shell script test
问题描述
我特林更新别人写的一个bash脚本,我已经翻过来一条线,我不知道。
I'm tring to update a bash script written by someone else and I've come accross a line I'm not sure about.
谁能告诉我下面的检查做什么:
Can anyone tell me what the following check does:
if [ :$RESULT != :0,0 ]
我以为它检查在$ result中的一些值,可能与子?
I assume it's checking for some value in $RESULT, possibly with a substring?
任何帮助AP preciated!
Any help appreciated!
推荐答案
命令 [
只是命令的别名测试
,右方括号只是被语法来糖(命令 [
忽略了最后一个参数,如果它是一个右括号),太行实际上读
The command [
is just an alias of the command test
, the closing square bracket just being sytax sugar (the command [
ignores the last argument if it's a closing bracket), so the line actually reads
if test :$RESULT != :0,0
它比较,如果该字符串:$结果
等于字符串:0,0
。冒号是该变量 $结果
是空的情况下prepended。该生产线将类似于以下如果结肠省略 $结果
是一个空字符串:
It compares if the string :$RESULT
equals to the string :0,0
. The colon is prepended for the case that the variable $RESULT
is empty. The line would look like the following if the colon was omitted and $RESULT
was an empty string:
if test != 0,0
这将导致一个错误,因为测试
预计前参数!=
。另一种方法是用引号,表明有一种说法,这是一个空字符串:
This would lead to an error, since test
expects an argument before !=
. An alternative would be to use quotes to indicate that there is an argument, which is an empty string:
if test "$RESULT" != 0,0
# Will become
if test "" != 0,0
你贴的变化是更轻便,虽然。
The variation you posted is more portable, though.
这篇关于shell脚本测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!