Shell 函数
linux shell 可以用户定义函数,然后在shell脚本中可以随便调用。
shell中函数的定义格式如下:
[function] funname [()]{ action; [return int;] }
说明:
- 1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
- 2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255
下面的例子定义了一个函数并进行调用:
#!/bin/bash demoFun(){ echo "This is your first shell function!"} echo "Function begin..." demoFun echo "Function end!
输出结果:
Functionbegin...Thisis your first shell function!Functionend!
下面定义一个带有return语句的函数:
#!/bin/bash funWithReturn(){ echo "The function is to get the sum of two numbers..." echo -n "Input first number: " read aNum echo -n "Input another number: " read anotherNum echo "The two numbers are $aNum and $anotherNum !"return $(($aNum+$anotherNum))} funWithReturn echo "The sum of two numbers is $? !"
输出类似下面:
Thefunctionis to get the sum of two numbers...Input first number:25Input another number:50The two numbers are 25and50!The sum of two numbers is75!
函数返回值在调用该函数后通过 $? 来获得。
注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。
函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...
带参数的函数示例:
#!/bin/bash funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" echo "The string of the parameters is $* !"} funWithParam 1234567893473
输出结果:
The value of the first parameter is1!The value of the second parameter is2!The value of the tenth parameter is10!The value of the tenth parameter is34!The value of the eleventh parameter is73!The amount of the parameters is12!Thestring of the parameters is1234567893473!"
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
另外,还有几个特殊字符用来处理参数:
$# | 传递到脚本的参数个数 |
$* | 以一个单字符串显示所有向脚本传递的参数 |
$$ | 脚本运行的当前进程ID号 |
$! | 后台运行的最后一个进程的ID号 |
$@ | 与$#相同,但是使用时加引号,并在引号中返回每个参数。 |
$- | 显示Shell使用的当前选项,与set命令功能相同。 |
$? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
本文转自:http://www.w3cschool.cc/linux/linux-shell-func.html
相关推荐
shell编程与普通的编程语言相比,shell编程可以用来执行一些流程调度以及任务量比较轻的处理流程的处理工具--因为毕竟是系统命令的整合调用,处理的时候,都会产生系统调用,在处理任务量大且比较复杂的流程的任务时...
shell 函数
①创建第一个shell脚本②运行shell脚本shell 基础语法 ①变量②变量分类与数组③shell传递参数④基本运算符shell流程控制语法 ①if else条件判断②for循环③while循环④until循环⑤case语句⑥跳出循环shell函数①...
关于vb6.0的 SHELL函数和串口通信实例
Linux中的shell函数Linux中的shell函数
简单的介绍SHELL函数的相关知识
linux运维学习笔记:Shell函数
day5shell函数和数组shell编程.txt
VBShell函数用法参照.pdf
VB中shell函数实例——大学生最需要的七类网站,学习用可以
tacklebox, 组织和共享 fish shell 函数的框架 Tacklebox问题组织和共享 shell 代码库和Fragment是很困难的。解决方案Tacklebox是 Fish shell 框架,它使得组织和共享有用的shell 功能。工具和主题集合变得很容易。...
Linux运维-运维系统服务04-Shell脚本d6-Shell函数与正则表达式-10函数中return作用1.mp4
ListView以及Windows的Shell函数显示Windows的程序组关于vb网络的编程基础知识winsock控件的使用,简单的聊天程序,局域网的网络编程
shell命令,结构性语句,shell函数,一些C语言关键字
linux shell 函数
一组Kubernetes原生dabbling的shell函数集合
LINUX原理及应用:Linux中的shell函数.ppt
composure - 撰写、文档、版本和组织您的shell函数
Linux运维-运维系统服务04-Shell脚本d6-Shell函数与正则表达式-07case应用案例1.mp4
Linux运维-运维系统服务04-Shell脚本d6-Shell函数与正则表达式-13综合案例需求说明.mp4