首页 > 系统服务 > 详细

在shell脚本中使用函数的返回值

时间:2017-07-27 19:59:48      阅读:302      评论:0      收藏:0      [点我收藏+]
#!/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" 

  

 

在shell脚本中使用函数的返回值

原文:http://www.cnblogs.com/oxspirt/p/7246428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!