function可以在腳本中起到簡化代碼,調(diào)用方便的用處
- 語法格式
function fname() {
程序段
}
函數(shù)設(shè)置一定要設(shè)置到腳本的最前面,因?yàn)槌绦蚴菑纳系较聢?zhí)行的。
- 例子
#!/bin/bash
function printit(){
echo "Your choice is $1" #這個(gè)$1是函數(shù)內(nèi)部的內(nèi)置函數(shù)。依賴下面指令的下達(dá)
}
echo "This progarm will print your selection !"
case $1 in
"one")
printit 1 #函數(shù)后面接參數(shù)
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage $0 {one|two|three}"
;;
esa
輸入 上面的選項(xiàng)就會出現(xiàn)對應(yīng)的輸出 如輸出
[root@k8s-node2 tmp]# ./show-fction.sh one
This progarm will print your selection !
Your choice is 1
學(xué)習(xí)筆記