Shell函数返回值,一般有3种方式:return、argv、echo
1、 return 语句
shell 函数的返回值,可以和其他语言的返回值一样,通过return语句返回。
示例:
#!/bin/bash -
function mytest()
{
echo "arg1 = $1"
if [ $1 = "1" ] ;then
return 1
else
return 0
fi
}
echo
echo "mytest 1"
mytest 1
echo $? # print return result
echo
echo "mytest 0"
mytest 0
echo $? # print return result
echo
echo "mytest 2"
mytest 2
echo $? # print return result
echo
echo "mytest 1 = "`mytest 1`
if mytest 1 ; then
echo "mytest 1"
fi
echo
echo "mytest 0 = "`mytest 0`
if mytest 0 ; then
echo "mytest 0&#