好好爱自己!

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

  

 

posted @ 2017-07-27 18:17  立志做一个好的程序员  阅读(5947)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处