shell脚本错误在哪里
#!/bin/bashtestfun()
{
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
不知原因.
谢谢给个提示。 将函数中return 改为echo应该可以通过 return的返回值在$?变量中 调用函数,使用函数的返回值,是像楼主这么使用吗??? 上面的脚本是参考 《shell编程从入门到精通》书上最后的例子。 此书是 张昊编著。
是我执行环境不对,还是书中例子垃圾? 这个问题用 echo 2解决不了。
其实本质的问题是:错误提示是由于 你用字符和数字进行了比较,这个是不能接受的。其中echo 2仍然会出错误。
可以 改为 [ expr 'testfun` > 1 ] 回复 1# sunrobin1
if [[ `testfun` -gt 1 ]]; then
......
页:
[1]