shell编程中使用函数库

来源:互联网 发布:飞客数据 编辑:程序博客网 时间:2024/06/07 16:56

在shell中使用函数库可以在多个脚本中使用相同的库函数,就相当于高级语言中的文件包含。

那么创建函数库的形式是:如函数库:myFuncs

#my Funcs,the is function librirayfunction First(){echo "First Functions"}function Second(){echo "Second Function"}function Three(){echo "Three Function"}function Four(){echo "Four Function"ls -l $HOME}

创建好以后使用命令source命令来编译;在shell中,对于source命令有一个快捷方式,使用点操作,使用它只是进行编译,在当前shell环境下允许,而不是创建新的shell来执行,使用创建的自定义库以后,在testso shell脚本中使用如:

#!/bin/bash#library test. ./myFuncs    #the  is connect libraryecho "lib test Begin...."First    #This is called the contents inside librarySecond<span style="white-space:pre"></span> #This is called the contents inside libraryThree<span style="white-space:pre"></span> #This is called the contents inside libraryFour     #This is called the contents inside libraryfunction name(){echo "name"}
输出结果是:

[soft01@localhost ~]$ ./testso lib test Begin....First FunctionsSecond FunctionThree FunctionFour Function总计 36drwxr-xr-x 2 soft01 soft01 4096 08-30 18:31 Desktop-rwxrwxrwx 1 soft01 soft01  201 08-30 18:51 myFuncs-rw-rw-rw- 1 soft01 soft01  199 08-30 18:51 myFuncs~-rwxrwxrwx 1 soft01 soft01  469 08-30 18:15 test-rw-rw-rw- 1 soft01 soft01  471 08-30 18:03 test~-rwxrwxrwx 1 soft01 soft01  123 08-30 18:51 testso-rw-rw-rw- 1 soft01 soft01  123 08-30 18:49 testso~-rw-rw-r-- 1 soft01 soft01  258 08-30 18:15 wen-rw-rw-r-- 1 soft01 soft01   17 08-30 15:53 zhou

这样就完成了shell自定义函数库的使用。