1. 函数
语法:
function 函数名 (){
}
写一个函数,功能是输出一句话“This is a function test”
[root@localhost script]# cat func.sh
#!/bin/bash
#Define your function here
function Hello(){
echo "This is a function test"
}
#Invoke your function
Hello
[root@localhost script]# chmod +x func.sh
[root@localhost script]# ./func.sh
This is a function test
注:函数的调用,直接写函数名即可实现对该函数的调用
2. 调用其他 shell 中的函数
可以把函数们放到一个文件中,若想用其他 shell 脚本中的函数,使用 source + 函数路径,即可实现
[root@localhost script]# ls
for2.sh for3.sh fun_all.sh func.sh ifping.sh if.sh ifwrong.sh until.sh while.sh
[root@localhost script]# cat fun_all.sh
function LOGAL(){
echo "---------------welcome to xiaotongzhi's family---------------"
}
function test1(){
echo "This is the function of test1"
}
function test2(){
echo "This is the function of test2"
}
[root@localhost script]# cat func.sh
#!/bin/bash
source ./fun_all.sh
#Define your function here
function Hello(){
echo "This is a function test"
}
test2
#Invoke your function
Hello
LOGAL
test1
[root@localhost script]# ./func.sh
This is the function of test2
This is a function test
---------------welcome to xiaotongzhi's family---------------
This is the function of test1
快乐学习,不足之处,欢迎指出,如需转载,请注明出处,谢谢