命令里面的if语句的bash脚本 [英] Command inside if statement of bash script
问题描述
我将以下行作为更大的bash脚本的一部分:
I have the following line as part of a much bigger bash script:
if [ `packages/TinySVM-0.09/bin/svm_learn 2>&1| grep TinySVM | wc -l | cut -c0-7 | sed 's/^ *//g'` -eq 1 ]
运行脚本后,我get:
upon running the script, I get:
./ install.sh:line 219:[: - eq:一元运算符预期
./install.sh: line 219: [: -eq: unary operator expected
其中第219行是上面的行。有关修复的任何建议吗?
Where line 219 is the line above. Any suggestions for a fix?
推荐答案
当您使用测试$ c $时会发生这种情况c>内置来自
[
,你的左侧表达式返回NUL。您可以通过以下方式解决此问题:
This happens when you are using the test
builtin via [
and your left side expression returns NUL. You can fix this by the use of:
if [ x`some | expression | here` = x1 ]; then
或者,因为你已经在使用bash,你可以使用它更好的(())
语法没有这个问题,并且:
Or, since you're already using bash you can use its much nicer (( ))
syntax which doesn't have this problem and do:
if (( $(some | expression | here) == 1 )); then
请注意,我还使用了 $()
用于反引号的命令替换``因为后者是非POSIX而且已弃用
Note that I also used $()
for command substitution over backticks `` as the latter is non-POSIX and deprecated
这篇关于命令里面的if语句的bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!