Shell⾼级篇
函数的作⽤与在于,当你需要重复的使⽤⼀段代码,如果是⼀⼩段代码重复还好,若是⼤段重复代码就太麻烦了。
函数就可以解决这个问题。
函数是定义⼀个代码块,且命名,然后可以在脚本⾥任何位置调⽤。
创建函数
案例
每次应⽤函数名 func1 ,shell就会执⾏func1⾥定义好的命令。
##函数简写
创建函数语法
function name {
commands
}
[root@chaogelinux shell]# cat test.sh
#!/bin/bash
# 超哥带你学shell
function func1 {
echo "This is an example of a function"
}
count=1
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func1
echo "Now this is the end of the script"
[root@chaogelinux shell]#
[root@chaogelinux shell]#
[root@chaogelinux shell]# bash test.sh
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is an example of a function
This is the end of the loop
This is an example of a function
Now this is the end of the script
[root@chaogelinux shell]# cat test.sh
www.apecome.com
猿来教育Linux云计算