当前位置: 技术问答>linux和unix
帮我分析一个shell的结果哦
来源: 互联网 发布时间:2017-03-26
本文导语: #!/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 myt...
#!/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给打印出来哦?
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给打印出来哦?
|
if不带[]是只测试后面命令运行是否成功,以$?来判断, 而不是测试0和1
因为mytest 0是返回0,if认为成功, mytest 1是返回1,if认为失败
所以
if mytest 0;then
echo "mytest 0";
fi
能输出mytest0
因为mytest 0是返回0,if认为成功, mytest 1是返回1,if认为失败
所以
if mytest 0;then
echo "mytest 0";
fi
能输出mytest0
|
0 不是成功么。。。