函數可以簡化程式的代碼量,達到更好的代碼復用度,因此會讓程式變得更加易讀、簡潔和易修改。其作用就是將需要多次使用的代碼整合到一塊,使其成為一個整體,然後通過函數名即可完成調用。 基本語法 其他簡化寫法如下 或 建議採用非簡化的書寫方式,方便閱讀代碼 函數執行 1、執行不帶 ...
函數可以簡化程式的代碼量,達到更好的代碼復用度,因此會讓程式變得更加易讀、簡潔和易修改。其作用就是將需要多次使用的代碼整合到一塊,使其成為一個整體,然後通過函數名即可完成調用。
基本語法
function functionName () {
語句
return n
}
其他簡化寫法如下
function functionName {
語句
return n
}
或
functionName () {
語句
return n
}
建議採用非簡化的書寫方式,方便閱讀代碼
函數執行
- 1、執行不帶參數的函數,直接輸入函數名即可,不需要帶括弧,如下所示:
functionName
- 執行函數時,函數名前的關鍵字function和函數名後面的()均不需要帶
- 函數的定義必須要在執行的程式前定義或載入
- 2、帶參數的函數執行語法如下所示:
functionName arg1 arg2
> - Shell中的位置參數($1/$2.../$#/$?/$@)均可以做為函數的參數進行傳遞
> - $0比較特殊,仍然是父腳本的名稱
> - 此時父腳本的參數會臨時被函數的參數所掩蓋或隱藏
> - 函數的參數變數是在函數體內裡面進行定義
函數的執行總結如下:
- 1、Shell各種程式的執行順序為:系統別名->函數->系統命令->可執行文件等
- 2、函數執行時,會和調用它的腳本共用變數,也可以為函數設定局部變數及特殊位置參數
- 3、在Shell函數裡面,return和exit功能類似,區別是return是退出函數,exit則是退出腳本
- 4、return語句會返回一個值給調用函數的程式,exit則會返回一個值給執行當前腳本的Shell
- 5、如果將函數單獨存放為一個文件,在載入時需要使用source或 . 進行載入
- 6、在函數內部一般使用local定義局部變數,僅在函數體內有效
函數示例
1、示例1:調用函數
[root@localhost Test]# cat testfunction.sh
#!/bin/bash
# first function
function HelloWorld() {
echo "Hello world"
}
# second function
Welcome() {
echo "Welcome to Shanghai"
}
# third function
function HelloShell {
echo "Hello Shell"
}
# invoke functions
HelloWorld # 調用函數
Welcome
HelloShell
[root@localhost Test]# bash testfunction.sh
Hello world
Welcome to Shanghai
Hello Shell
2、示例2:從文件中調用函數
[root@localhost Test]# cat invokefunction.sh
function Sum () {
for((i=1;i<=100;i++))
do
((sum=sum+i))
done
echo '{1..100} sum is :' $sum
}
[root@localhost Test]# cat invokefunctionfromfile.sh
#!/bin/bash
path="/root/Test/invokefunction.sh"
if [ -f ${path} ]
then
source $path # 載入函數
Sum # 調用函數
else
echo "file not exist or error"
fi
[root@localhost Test]# bash invokefunctionfromfile.sh
{1..100} sum is : 5050
3、示例3:函數參數傳遞
[root@localhost Test]# cat functionwithargs.sh
#!/bin/bash
function Add () { # 定義函數
((sum=$1+$2))
echo "$1 + $2 sum is" ${sum}
}
Add $1 $2 # 調用函數並傳遞參數
[root@localhost Test]# bash functionwithargs.sh 100 150
100 + 150 sum is 250
[root@localhost Test]# bash functionwithargs.sh 509 150
509 + 150 sum is 659
4、示例4:使用return返回函數運行結果
[root@localhost Test]# cat functionwithreturn.sh
#!/bin/bash
function TestReturn() {
if [ -d $1 ]
then
return "122"
else
return "222"
fi
}
TestReturn $1
result=$? # 獲取函數返回值
if [ ${result} == "122" ]
then
echo "$1 exist ,return value is:" ${result}
else
echo "$1 not exist ,return value is:" ${result}
fi
[root@localhost Test]# bash functionwithreturn.sh /etc/sysconfiggg
/etc/sysconfiggg not exist ,return value is: 222
[root@localhost Test]# bash functionwithreturn.sh /etc/sysconfig
/etc/sysconfig exist ,return value is: 122
在該示例中,主要通過$?獲取返回值,但返回值的範圍只能是0~255
5、示例5:使用echo返回函數運行結果
[root@localhost Test]# cat functionwithecho.sh
#!/bin/bash
function TestReturn() {
if [ -d $1 ]
then
echo "122"
else
echo "222"
fi
}
result=$(TestReturn $1) # 獲取函數返回值
if [ ${result} == "122" ]
then
echo "$1 exist ,return value is:" ${result}
else
echo "$1 not exist ,return value is:" ${result}
fi
[root@localhost Test]# bash functionwithecho.sh /etc/sysconfig
/etc/sysconfig exist ,return value is: 122
[root@localhost Test]# bash functionwithecho.sh /etc/sysconfiggg
/etc/sysconfiggg not exist ,return value is: 222
在該示例中,主要使用$()獲取返回值,在該方法中,沒有範圍限制,是一種比較安全的返回方式。
[root@localhost Test]# cat functionwithecho.sh
#!/bin/bash
function TestReturn() {
if [ -d $1 ]
then
echo "$1 exist"
else
echo "$1 not exist"
fi
}
result=$(TestReturn $1) # 獲取返回值,返回的結果是字元串
if [ "${result}" == "$1 exist" ]
then
echo "$1 exist ,return value is:" ${result}
else
echo "$1 not exist ,return value is:" ${result}
fi
[root@localhost Test]# bash functionwithecho.sh /etc/sysconfiggg
/etc/sysconfiggg not exist ,return value is: /etc/sysconfiggg not exist
[root@localhost Test]# bash functionwithecho.sh /etc/sysconfig
/etc/sysconfig exist ,return value is: /etc/sysconfig exist
本文同步在微信訂閱號上發佈,如各位小伙伴們喜歡我的文章,也可以關註我的微信訂閱號:woaitest,或掃描下麵的二維碼添加關註: