Shell程式設計-09-Shell中的函式
函式可以簡化程式的程式碼量,達到更好的程式碼複用度,因此會讓程式變得更加易讀、簡潔和易修改。其作用就是將需要多次使用的程式碼整合到一塊,使其成為一個整體,然後通過函式名即可完成呼叫。
基本語法
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,或掃描下面的二維碼新增關注:
