⑥.shell 函数

什么是shell函数 把一堆命令做成一个函数 方便后面代码调用
1)定义方式:

方式一
函数() {
   command  ...
}

方式二
function 函数 {
   command  ...
}

2)传参数
位置参数传参

#!/bin/bash
fun01() {
    echo "hello $1"
}
fun01 

[root@famous-machine-1 ~]# sh test.sh test
hello 

位置参数向函数位置传参传(参数是可变的)

[root@famous-machine-1 ~]# cat test.sh 
#!/bin/bash
fun01() {
    echo "hello $num"
}
num=$1
fun01 

[root@famous-machine-1 ~]# sh test.sh  33
hello 33

脚本的位置参数不等于函数的位置参数

#!/bin/bash
fun01() {
    echo "hello $1"
}
fun01 $1
fun01 $2
fun01 $3
fun01 $4

[root@famous-machine-1 ~]# sh test.sh  test1 test2 test3 test4 
hello test1
hello test2
hello test3
hello test4

3)函数返回状态

原文地址:https://www.cnblogs.com/yangtao416/p/14812406.html