在Linux shell中,函数这个特性很类似于JS,都需要先定义后才能调用。
来一段简单的find函数
#!/bin/bash
#findit
findit()
{
if [ $# -lt 1 ]
then
echo "no more than one arguments is valid"
return 1
fi
find -name $1 -print
}
这段函数实现接受一个参数,并查找一个文件。
如果要将这段函数载入shell,供其他人使用。则需要像下面这样做。
先输入set | grep findit,正常情况下会输出空之类的东西。输入findit命令也会提示找不到findit命令。
这时候使用如下命令
<点><空格><函数路径><函数名>
即可将函数导入当前shell中
对应我的机器上,则需要执行
[chenwu@localhost unit9-function]$ ls
functions.main helloWorld.sh
[chenwu@localhost unit9-function]$ . ./functions.main
[chenwu@localhost unit9-function]$
再执行findit 就会提示你正确的结果了。