在shell中使用函数库可以在多个脚本中使用相同的库函数,就相当于高级语言中的文件包含。
那么创建函数库的形式是:如函数库:myFuncs
#my Funcs,the is function libriray
function 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 library
echo "lib test Begin...."
First #This is called the contents inside library
Second<span style="white-space:pre"> </span> #This is called the contents inside library
Three<span style="white-space:pre"> </span> #This is called the contents inside library
Four #This is called the contents inside library
function name(){
echo "name"
}
输出结果是:
[soft01@localhost ~]$ ./testso
lib test Begin....
First Functions
Second Function
Three Function
Four Function
总计 36
drwxr-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自定义函数库的使用。