shell 函數返回值問題

方法一: 使用全局變量

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. g_result=""  
  2.   
  3. function testFunc()  
  4. {  
  5.     g_result='local value'  
  6. }  
  7.   
  8. testFunc  
  9. echo $g_result  

方法二: 把shell函數作爲子程序調用,將其結果寫到子程序的標準輸出

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. function testFunc()  
  2. {  
  3.     local_result='local value'  
  4.     echo $local_result  
  5. }  
  6.   
  7. result=$(testFunc)  
  8. echo $result  


方法三: 用$?

add()
{
sum=$(($1+$2))
return $sum
}

add 1 2
echo $?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章