日期:2014-05-16  浏览次数:20660 次

帮我分析一个shell的结果哦
#!/bin/bash -
function mytest(){
echo "arg1 = $1"
if [ $1 = "1" ];then
   return 1;
else
   return 0;
fi
}

echo
echo "mytest 0 = "`mytest 0`;
if mytest 0;then
echo "mytest 0";
fi
echo "end";


这个shell运行结果是
mytest 0 = arg1 =0
arg1 =0

mytest 0
end

但是,我觉得,那红色标记部分不应该打印出来呀,为什么 if后,能把 mytest 0给打印出来哦?


------解决方案--------------------
0 不是成功么。。。
------解决方案--------------------
if不带[]是只测试后面命令运行是否成功,以$?来判断, 而不是测试0和1

因为mytest 0是返回0,if认为成功, mytest 1是返回1,if认为失败

所以
if mytest 0;then
echo "mytest 0";
fi
能输出mytest0