SHELL脚本编程-函数
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.函数介绍
函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程
它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部分
函数和shell程序比较相似,区别在于
Shell程序在子Shell中运行
而Shell函数在当前Shell中运行。因此在当前Shell中,函数可以对shell中变量进行修改
二.定义函数
[root@node101.yinzhengjie.org.cn ~]# help function #函数由两部分组成:函数名和函数体 function: function name { COMMANDS ; } or name () { COMMANDS ; } Define shell function. Create a shell function named NAME. When invoked as a simple command, NAME runs COMMANDs in the calling shell's context. When NAME is invoked, the arguments are passed to the function as $1...$n, and the function's name is in $FUNCNAME. Exit Status: Returns success unless NAME is readonly. [root@node101.yinzhengjie.org.cn ~]#