在 bash 4.2.8(1)-release
(x86_64-pc-linux-gnu
) 在 Ubuntu 11.04 上这个命令
[ $(wc -l /var/www/some.log|cut -d " " -f 1) -le 6 ] || echo "no"
产量
echo: command not found
只要 ||
子句生效。 &&
子句有效。我试过更改顺序(||
第一,&&
第一)无济于事。
有趣的是,
true && echo "yes" || echo "no"
产量
yes
和
false && echo "yes" || echo "no"
产量
no
所以我想我的测试子句有问题。我想检查 some.log 的文件长度是否小于或等于 6,如果不是,则执行其他操作,如果是,则执行其他操作。
测试子句中的双括号也不起作用。
不过,长格式的测试确实有效:
test $(wc -l /var/www/some.log|cut -d " " -f 1) -le 6 && echo "yes" || echo "no"
yes
有趣吧?
一如既往,感谢您对此的任何提示/提示。
Christian.
最佳答案
试试这个(使用 2 [ 和 ]):
[[ $(wc -l /var/www/some.log|cut -d " " -f 1) -le 6 ]] || echo "no"
^^ ^^
来自 bash 的人:
Compound Commands
...
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS.
我想单个 [ 和 ] 不是执行测试的标准,即使它对我有效。
编辑:
实际上,[ 和 ] 是一种标准的测试方式,它是 bash 中的内置命令。如果 [[ ... ]] 有效而 [ ... ] 无效,我不会完全理解它。它可能与在 [ 和 ] 中执行/不执行的扩展有关。
关于bash 真/假重定向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090054/