linux shell自定义函数(定义,返回值,变量作用域)

简介:
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。下面说说它的定义方法,以及调用需要注意那些事项。
 
一、定义shell函数(define function)
InBlock.gif[ function ] funname [()]    
InBlock.gif 
InBlock.gif{    
InBlock.gif 
InBlock.gif        action;    
InBlock.gif 
InBlock.gif        [ return  int;]    
InBlock.gif 
InBlock.gif}    
InBlock.gif 
InBlock.gif说明:    
InBlock.gif 
InBlock.gif1、可以带function fun()    定义,也可以直接fun() 定义,不带任何参数。    
InBlock.gif 
InBlock.gif2、参数返回,可以显示加: return 返回,如果不加,将以最后一条命令运行结果,作为返回值。  return后跟数值n(0-255    
InBlock.gif 
 
InBlock.gif实例(testfun1.sh): 
InBlock.gif 
InBlock.gif#!/bin/sh     
InBlock.giffSum 3 2;     
InBlock.giffunction fSum()     
InBlock.gif{     
InBlock.gif            echo $1,$2;     
InBlock.gif 
InBlock.gif             return $(($1+$2));     
InBlock.gif 
InBlock.gif}     
InBlock.gif 
InBlock.giffSum 5 7;     
InBlock.gif 
InBlock.giftotal=$(fSum 3 2);     
InBlock.gif 
InBlock.gifecho $total,$?;     
InBlock.gif 
InBlock.gif                                            
InBlock.gif 
InBlock.gifsh testfun1.sh     
InBlock.gif 
InBlock.giftestfun1.sh: line 3: fSum: command not found     
InBlock.gif5,7     
InBlock.gif3,2    
InBlock.gif    
InBlock.gif 
InBlock.gif从上面这个例子我们可以得到几点结论: 
InBlock.gif 
InBlock.gif1、必须在调用函数地方之前,声明函数,shell脚本是逐行运行。不会像其它语言一样先预编译。一次必须在使用函数前先声明函数。 
InBlock.gif 
InBlock.gif2、total=$(fSum 3 2);    通过这种调用方法,我们清楚知道,在shell 中 单括号里面,可以是:命令语句。 因此,我们可以将shell中函数,看作是定义一个新的命令,它是命令,因此 各个输入参数直接用 空格分隔。 一次,命令里面获得参数方法可以通过:$0…$n得到。 $0代表函数本身。    
InBlock.gif 
InBlock.gif3、函数返回值,只能通过$? 系统变量获得,直接通过=,获得是空值。其实,我们按照上面一条理解,知道函数是一个命令,在shell获得命令返回值,都需要通过$?获得。 
InBlock.gif 
 
二、函数作用域,变量作用范围
InBlock.gif先我们看一个实例(testfun2.sh ): 
InBlock.gifview sourceprint?01 #!/bin/sh     
InBlock.gifecho $(uname);     
InBlock.gif 
InBlock.gifdeclare num=1000; 
InBlock.gif     
InBlock.gifuname()     
InBlock.gif{     
InBlock.gif     echo  "test!";     
InBlock.gif     ((num++));     
InBlock.gif      return 100;     
InBlock.gif}     
InBlock.gif 
InBlock.giftestvar()     
InBlock.gif{     
InBlock.gif 
InBlock.gif         local num=10;     
InBlock.gif         ((num++));     
InBlock.gif         echo $num;     
InBlock.gif}            
InBlock.gifuname;     
InBlock.gifecho $?     
InBlock.gifecho $num;     
InBlock.giftestvar;     
InBlock.gifecho $num;     
InBlock.gifsh testfun2.sh     
InBlock.gifLinux     
InBlock.giftest!     
InBlock.gif100     
InBlock.gif1001     
InBlock.gif11     
InBlock.gif1001    
 
 




本文转自 fenghao.cn 51CTO博客,原文链接:http://blog.51cto.com/linuxguest/545865,如需转载请自行联系原作者
目录
相关文章
|
16天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
14天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
14天前
|
Shell 应用服务中间件 nginx
shell学习(七) 【shell 函数】
shell学习(七) 【shell 函数】
8 1
|
17天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
23天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
15 0
|
30天前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
50 0
|
30天前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1
|
7月前
|
Shell
shell编程之函数
shell编程之函数
44 1
|
3月前
|
Shell
Shell 编程快速入门 之 函数基础知识
Shell 编程快速入门 之 函数基础知识
67 0
Shell 编程快速入门 之 函数基础知识