bash 真/假重定向错误

标签 bash testing

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/

相关文章:

swift - 如何检查 os x 中的框架可用性?

linux - 你如何给su当前用户环境变量

testing - postman 中的循环邮寄请求

ios - 在苹果商店中部署带有 strip 集成的应用程序

testing - Ember-auth 登录测试因 json 失败

ruby-on-rails - 如何避免 Cucumber 中不明确的步骤定义?

bash - 使用包含引号的字符串作为 bash 中命令的参数,无需额外转义

bash - 调整 Bash 'cd' 和 TAB 完成以识别拼写错误

bash:使用 expect 来自动化 GPG 签名

Django 测试如何断言重定向