在shell腳本中使用函數的返回值


#!/bin/bash -  
function mytest()
{
    echo "arg1 = $1"  
    if [ $1 = "1" ] ;then
        return 1
    else
        return 0
    fi
}
if mytest 2; then
        echo "aaaaaaaaaa"
fi

  執行結果:

稍微改一下

#!/bin/bash -
function mytest()
{
  echo "arg1 = $1"
  if [ $1 = "1" ] ;then
    return 1
  else
    return 0
  fi
}
if mytest 1; then
  echo "aaaaaaaaaa"
fi

 

 

---------------------------------------------------------------------------

shell 中定義的變量是全局的,函數上面定義的變量在函數內部仍然是可見的

#!/bin/bash -  
  
g_var=  
function mytest2()  
{  
    echo "mytest2"  
    echo "args $1"  
    g_var=$1  
  
    return 0  
}  
  
mytest2 1  
echo "return $?"  
  
echo  
echo "g_var=$g_var" 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM