函数:写一个代码快用来重复调用
一、函数的写法格式
1. function check_cpu {
cat /proc/cpuinfo
}
ls /etc/
check_cpu 【不调用函数,则函数体中的命令是不会执行的;在哪里调用,函数体中的命令就在那里执行】
2. *fun_name() {
函数体
$1 $2 ... $10
}
fun_name 1 2 3
: wq
function.sh 4 5 6 7 8 9 10 11 12 13 14
参数如果在函数体外面可以直接使用
二、变量
local本地变量
loacl i=1
如果在函数体外也定义了一个相同的变量,不生效
扩展:source/bash/chmod+x全路径 【父子进程】
三、return
结束函数体执行
和exit的区别
return和exit都指定的返回值 0代表执行成功,1-255代表执行失败
return只用于函数体中;
四、迭代
迭代必须要指定初始值