sunrobin1 发表于 2013-04-17 23:03

shell脚本错误在哪里

#!/bin/bash
testfun()
{
return 2
}

while true; do
if [ `testfun` -gt 1 ]; then
    echo "great than 1"
else   
    echo "less 1"
fi
sleep 3
done

以上保存为 test.sh
运行 . test.sh

提示 bash: [: -gt: unary operator expected
不知原因.   

谢谢给个提示。

stone435 发表于 2013-04-18 08:55

将函数中return 改为echo应该可以通过

jack1007 发表于 2013-04-18 09:12

return的返回值在$?变量中

happyluren 发表于 2013-04-18 13:24

调用函数,使用函数的返回值,是像楼主这么使用吗???

sunrobin1 发表于 2013-04-18 22:24

上面的脚本是参考 《shell编程从入门到精通》书上最后的例子。 此书是 张昊编著。
是我执行环境不对,还是书中例子垃圾?

gulang2004xy 发表于 2013-04-20 19:09

这个问题用 echo 2解决不了。
其实本质的问题是:错误提示是由于 你用字符和数字进行了比较,这个是不能接受的。其中echo 2仍然会出错误。
可以 改为 [ expr 'testfun` > 1 ]

xiaolanyu1 发表于 2013-04-21 17:26

回复 1# sunrobin1


    if [[ `testfun` -gt 1 ]]; then
       ......
页: [1]
查看完整版本: shell脚本错误在哪里