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...

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM