随笔-314  评论-209  文章-0  trackbacks-0

方法一: 使用全局变量

  1. g_result=""  
  2.   
  3. function testFunc()  
  4. {  
  5.     g_result='local value'  
  6. }  
  7.   
  8. testFunc  
  9. echo $g_result  

方法二: 把shell函数作为子程序调用,将其结果写到子程序的标准输出

  1. function testFunc()  
  2. {  
  3.     local_result='local value'  
  4.     echo $local_result  
  5. }  
  6.   
  7. result=$(testFunc)  
  8. echo $result  

看到一篇关于函数返回值的好文章,分享一下: http://www.linuxjournal.com/content/return-values-bash-functions 
posted on 2015-09-21 10:20 xzc 阅读(4412) 评论(2)  编辑  收藏 所属分类: linux/unix

评论:
# re: 【shell】获取函数返回值的两种方法 2015-09-21 11:35 | xzc
function myfunc()
{
local myresult='some value'
echo "$myresult"
}

result=$(myfunc) # or result=`myfunc`
echo $result  回复  更多评论
  
# re: 【shell】获取函数返回值的两种方法 2015-09-21 11:35 | xzc
function myfunc()
{
local __resultvar=$1
local myresult='some value'
eval $__resultvar="'$myresult'"
}

myfunc result
echo $result  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: