Function shell脚本中的函数/bin/false产生负面影响

Function shell脚本中的函数/bin/false产生负面影响,function,shell,Function,Shell,我很困惑: #!/bin/sh [ -f /etc/init.d/functions ] && . /etc/init.d/functions [ 0 -eq 0 ] && action "Test" /bin/false || action "Test" /bin/true echo "###############" [ 0 -eq 0 ] && action "Test" /bin/true || action "Test" /bin/

我很困惑:

#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions

[ 0 -eq 0 ] && action "Test" /bin/false || action "Test" /bin/true

echo "###############"

[ 0 -eq 0 ] && action "Test" /bin/true || action "Test" /bin/false
结果是:

Test                                                       [FAILED]
Test                                                       [  OK  ]
###############
Test                                                       [  OK  ]
action/bin/false函数是否返回使| |后面的语句执行的假值?
如果我必须在“&&”块中放入/bin/false,该怎么办

既然/bin/false返回false,它将通过| |并返回/bin/true

这样看:

true && false || true -> true
true && true || false -> true
[ 0 -eq 0 ] && { action "Test" /bin/false || action "Test" /bin/true; }
如果你使用

[ 0 -eq 0 ] && action "Test" /bin/false && action "Test" /bin/true
如您所料,if将返回false

看到这个了吗

#!/bin/bash
[ 1 = 1 ] && echo "displayed because previous statement is true"

[ 1 = 0 ] && echo "not shown because previous statement is false"

[ 1 = 1 ] || echo "not shown because previous statement is true"

[ 1 = 0 ] || echo "displayed because previous statement is false"

由于/bin/false返回false,它将通过| |并返回/bin/true

这样看:

true && false || true -> true
true && true || false -> true
[ 0 -eq 0 ] && { action "Test" /bin/false || action "Test" /bin/true; }
如果你使用

[ 0 -eq 0 ] && action "Test" /bin/false && action "Test" /bin/true
如您所料,if将返回false

看到这个了吗

#!/bin/bash
[ 1 = 1 ] && echo "displayed because previous statement is true"

[ 1 = 0 ] && echo "not shown because previous statement is false"

[ 1 = 1 ] || echo "not shown because previous statement is true"

[ 1 = 0 ] || echo "displayed because previous statement is false"
问题是:

action "Test" /bin/false
返回使
|
之后的命令作为失败操作执行的
1
有效地其行为如下:

true && false || true -> true
true && true || false -> true
[ 0 -eq 0 ] && { action "Test" /bin/false || action "Test" /bin/true; }
这是使用
if/else/fi
并获得正确行为的更多原因:

echo "###############"
if [ 0 -eq 0 ]; then
   action "Test" /bin/false
else
   action "Test" /bin/true
fi
这将输出:

Test                                                       [FAILED]
问题是:

action "Test" /bin/false
返回使
|
之后的命令作为失败操作执行的
1
有效地其行为如下:

true && false || true -> true
true && true || false -> true
[ 0 -eq 0 ] && { action "Test" /bin/false || action "Test" /bin/true; }
这是使用
if/else/fi
并获得正确行为的更多原因:

echo "###############"
if [ 0 -eq 0 ]; then
   action "Test" /bin/false
else
   action "Test" /bin/true
fi
这将输出:

Test                                                       [FAILED]