码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-07-27 20:29:53      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:--   div   http   echo   highlight   images   logs   使用   alt   

#!/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脚本中使用函数的返回值

标签:--   div   http   echo   highlight   images   logs   使用   alt   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!