在Shell中使用函数文件
需要编写一个较庞大的脚本时,可能会涉及许多函数、变量。这是通常建议将众多的函数、变量放入一个单独的脚本内。这样做的好处很明显,不用担心某个函数、变量是否已经被定义和使用,也不用频繁地定义、清除函数和变量。
一、函数文件的编写
函数文件的格式和脚本文件一样。
[root@localhost shell]# cat function.example
#函数文件中也可以不写下面这行Shell调用语句
#!/bin/bash #function example.
#This is a function definition script.
#2013/12/17 hello()
{
echo "Now is the function hello."
echo "Hello! $1."
return
} function hi()
{
echo "Now si the function hi."
echo "Hi! $1."
return
}
二、函数文件的调用
要调用已经编写好的函数文件,可以像定制工作环境那样,将函数文件包含在脚本文件中,然后直接进行调用。
(1)下面引入调用函数文件的示例脚本call_func_file.sh。在这个脚本中使用执行的方式调用函数文件,然后再直接调用函数文件中的函数。
[root@localhost shell]# cat call_func_file.sh
#!/bin/bash #This is a example script.
#2013/12/17 #function.example
. ./function.example #call function hell
echo "Now call the function hello."
hello Jhon #call function hi
echo "Now call the function hi."
hi Alix
从上面的示例文件中可以看出,调用函数文件的格式为:
. ./function.example
使用这种方式调用函数文件时,函数文件的路径应该与脚本文件的路径相同,否则在调用时应该使用函数文件的绝对路径或相对路径。
执行示例脚本:
[root@localhost shell]# ./call_func_file.sh
Now call the function hello.
Now is the function hello.
Hello! Jhon.
Now call the function hi.
Now is the function hi.
Hi! Alix.
(2)除了上面介绍的调用方式外,函数文件也可以像变量一样在命令提示符下使用。在命令提示符中调用函数文件后,就可以直接调用函数。
在命令提示符中调用函数文件:
[root@localhost shell]# . ./function.example
[root@localhost shell]# hi Alix
Now is the function hi.
Hi! Alix.
[root@localhost shell]# hello Alix
Now is the function hello.
Hello! Alix.
用户也可以使用上面的方法,将函数文件写入用户的环境变量配置文件中,然后像使用命令一样调用自定义的函数,以实现较复杂的功能。
在Shell中使用函数文件的更多相关文章
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- shell中的函数、shell中的数组、告警系统需求分析
7月16日任务 20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析 20.16/20.17 shell中的函数 函数就是一个子shell就是一个代码段 ...
- Linux centosVMware shell中的函数、shell中的数组、
一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...
- shell中的函数、数组、报警系统脚本
1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond} ...
- 小苏的Shell编程笔记之六--Shell中的函数
http://xiaosu.blog.51cto.com/2914416/531247 Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以 ...
- Shell中的函数
一.在脚本中定义函数 与变量一样,在使用函数之前应该对函数进行定义.与其他编程类语言相比,由于没有数据类型的概念,因此也不必定义函数的类型. (1)在脚本中可以使用以下方式定义函数: function ...
- shell中使用函数
函数定义.调用 $ cat te.sh #!/bin/bash # define a function test() { echo "This is a function." } ...
- shell中的函数、数组
函数定义: 数组:
- shell中的函数 shell中的数组 告警系统需求分析
随机推荐
- Hadoop2.3+Hive0.12集群部署
0 机器说明 IP Role 192.168.1.106 NameNode.DataNode.NodeManager.ResourceManager 192.168.1.107 Secondary ...
- 《Java数据结构与算法》笔记-CH5-链表-4用链表实现堆栈
//用链表实现堆栈 /** * 节点类 */ class LinkS { private long data; public LinkS next; public LinkS(long d) { th ...
- 未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。
未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个 ...
- MVC中使用AuthorizeAttribute做身份验证操作
代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...
- -g vs -rdynamic
[-g vs -rdynamic] -g选项与-rdynamic选项的差别:1,-g选项新添加的是调试信息(一系列.debug_xxx段),被相关调试工具,比如gdb使用,可以被strip掉. 2,- ...
- Linux 修改hostname 文件
linux 的机器修改hostname: 修改 /etc/hosts 修改 /etc/sysconfig/network 重启机器reboot
- Ubuntu安装Burg
友情提示:本文只介绍了如何安装Burg,没有关于卸载Burg的相关说明.事实上,我后来直接新装了12.04,我没有卸载Burg的经验.考虑到Burg事关系统引导的大事,安装的话按本文来做应该没有问题, ...
- does not support ASP.NET compatibility 错误
The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibi ...
- Android 当媒体变更后,通知其他应用重新扫描
在媒体文件改变后 发出 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE广播,告知其他应用,媒体文件发生改变. 具体代码片段: File oldFile = new File ...
- 行为类模式(一):职责链(Chain Of Responsibility)
定义 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系. 将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. UML 优点 将请求的发送者和接收者解耦 可以 ...