Linux教程

linux中创建自定义函数、加载自定义函数、调用自定义函数

本文主要是介绍linux中创建自定义函数、加载自定义函数、调用自定义函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

1、创建自定义函数

格式:

function name {

  command

}

[root@rhel7pc1 test]# ls
test.sh
[root@rhel7pc1 test]# cat test.sh   ## shell中创建函数格式
#!/bin/bash
function fun_test {
        echo "hello world!"
}

 

2、加载、调用函数

[root@rhel7pc1 test]# ls
test.sh
[root@rhel7pc1 test]# cat test.sh
#!/bin/bash
function fun_test {
        echo "hello world!"
}
[root@rhel7pc1 test]# fun_test              ## 函数未加载前
bash: fun_test: command not found...
[root@rhel7pc1 test]# source test.sh        ## 加载函数
[root@rhel7pc1 test]# fun_test              ## 调用函数
hello world!

 

3、取消函数加载

[root@rhel7pc1 test]# fun_test         ## 调用函数
hello world!
[root@rhel7pc1 test]# unset fun_test   ## 取消函数加载
[root@rhel7pc1 test]# fun_test         ## 无法调用函数
bash: fun_test: command not found...

 

这篇关于linux中创建自定义函数、加载自定义函数、调用自定义函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!