基本信息
文件名称:linux基础-17shell编程-函数+课件.pptx
文件大小:76.45 KB
总页数:6 页
更新时间:2025-05-26
总字数:约小于1千字
文档摘要

shell编程-函数

课程目录shell函数调用函数变量作用域

3函数的所有标准输出都传递给了主程序的变量获取函数的返回的状态value_name=`function_name[arg1arg2…]`函数调用格式:function_name[arg1arg2…]echo$?方式1:方式2:shell函数调用

4实例check_user(){#查找已登录的指定用户 user=`who|grep$1|wc-l`if[$user–eq0]thenreturn0#未找到指定用户elsereturn1#找到指定用户fi}whiletrue#MAIN,Main,main:programbeginheredo echoInputusername:\c readuname check_user$uname#调用函数,并传递参数uname if[$?–eq1]#$?为函数返回值 then echouser$unameonline else echouser$unameoffline fidone

5函数变量作用域全局作用域:在脚本的其他任何地方都能够访问该变量。局部作用域:只能在声明变量的作用域内访问。声明局部变量的格式:Localvariable_name=value

6函数变量作用域Scope(){Locallclvariable=1Gblvariable=2echo“lclavariableinfunction=$lclvariable”echo“Gblvariableinfunction=$Gblvariable”}Scopeecho“lclavariableinfunction=$lclvariable”echo“Gblvariableinfunction=$Gblvariable”