bestchenwu

    在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  就会提示你正确的结果了。

 

 

分类:

技术点:

相关文章:

  • 2021-10-30
  • 2021-11-09
  • 2022-01-21
  • 2021-05-18
  • 2022-12-23
  • 2021-11-20
  • 2021-05-23
  • 2021-09-01
猜你喜欢
  • 2021-07-30
  • 2021-09-06
  • 2021-09-08
  • 2022-01-17
  • 2021-06-15
  • 2022-01-31
  • 2022-02-09
相关资源
相似解决方案