#!/bin/bash
#函数返回值:
#1. return(0-255) 通过$?获得
#2. echo 通过`function`获得,非结果echo到文件或者/dev/null ,返回值是数组,字符串,大于255的整数,可以使用echo
#3. 声明全局变量 declare 返回值是数组,字符串,大于255的整数,可以使用echo
# echo 最好用,返回数组也要依靠echo
declare g_res
function test1 {
local a=1
local b=2
local c=$[$a+$b]
return $c
}
function test2 {
local a=4
if [ $a -eq 4 ]
then
echo "four"
else
echo " null"
fi
}
function test3 {
g_res=1
}
function test4 {
local i
local arr
for (( i=0; i<5; ++i))
do
arr[$i]=$i
done
echo ${arr[*]}
}
test1
echo "function test1 return : "$?
test2Result=`test2`
echo "function test2 return :"$test2Result
test3
echo "function test3 return : "$g_res
test4Res=`test4`
#函数返回值:
#1. return(0-255) 通过$?获得
#2. echo 通过`function`获得,非结果echo到文件或者/dev/null ,返回值是数组,字符串,大于255的整数,可以使用echo
#3. 声明全局变量 declare 返回值是数组,字符串,大于255的整数,可以使用echo
# echo 最好用,返回数组也要依靠echo
declare g_res
function test1 {
local a=1
local b=2
local c=$[$a+$b]
return $c
}
function test2 {
local a=4
if [ $a -eq 4 ]
then
echo "four"
else
echo " null"
fi
}
function test3 {
g_res=1
}
function test4 {
local i
local arr
for (( i=0; i<5; ++i))
do
arr[$i]=$i
done
echo ${arr[*]}
}
test1
echo "function test1 return : "$?
test2Result=`test2`
echo "function test2 return :"$test2Result
test3
echo "function test3 return : "$g_res
test4Res=`test4`
echo "function test4 return : "$test4Res
运行结果:
function test1 return : 3
function test2 return :four
function test3 return : 1
function test4 return : 0 1 2 3 4