在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" 

  

原文地址:https://www.cnblogs.com/oxspirt/p/7246428.html