如果[0]返回true。为什么? [英] if [ 0 ] returns true. why?
本文介绍了如果[0]返回true。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在学习shell脚本。无论如何,我运行下面的程序。
I'm now learning shell-script. Anyway, I run the below program.
#!/bin/bash
sudo rmmod my_driver
make clean
make
sudo insmod my_driver.ko // return value is 0
var=$?
if [ $var ];
then
echo " $var, not done!!"
else
echo " $var, done!!"
fi
输出是,
...
make[1]: Leaving directory `/usr/src/kernels/2.6.32-431.11.2.el6.x86_64'
0, not done!!
在C语言(我相信任何语言),如果如果
条件返回假
它会执行其他
块。
In C language(I believe in any language), if if
condition returns false
it'll execute else
block.
I tried below conditions too, but I faced same problem
if [ "$var" ]
if [ "$var" -eq "0" ]
if [ $var -eq 0 ]
if [ $? ]
为什么不执行其他
阻止?
我怎么能执行其他
块如果[0]
?
推荐答案
传递的任何的字符串到测试
命令,即 [
,会成功。因此,
Passing any string to the test
command, i.e. [
, would succeed. So
if [ 0 ]
等同于
if [ 1 ]
或
if [ foobar ]
您可以利用算术背景下,而不是,并说:
You could make use of the arithmetic context instead, and say:
if ((var)); then
或使用测试
命令:
if [ $var -eq 0 ]; then
这篇关于如果[0]返回true。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文