在bash中定義函數(shù)
function和函數(shù)名字可以任意省略一個(gè)
需要注意的是,函數(shù)必須在被調(diào)用之前定義
如果沒(méi)有特別標(biāo)注,bash中的變量都是全局變量,即使定義在函數(shù)內(nèi)部,所以在函數(shù)外部也可以使用;反過(guò)來(lái)也一樣,在函數(shù)外部定義的變量在函數(shù)內(nèi)部也可以使用。例如在函數(shù)內(nèi)部修改變量
#!/bin/bash update_prefecture() { prefecture=Aomori ←----- 修改在函數(shù)外部定義的變量的值 echo "$prefecture" } prefecture=Hokkaido ←----- 定義變量(全局變量) echo "$prefecture" update_prefecture echo "$prefecture"
結(jié)果是
若想定義只作用于函數(shù)內(nèi)部的局部變量可以加local字段
local 變量名=值
對(duì)于嵌套調(diào)用的兩個(gè)函數(shù),在外部函數(shù)中定義一個(gè)局部變量prefecture,然后在內(nèi)部被調(diào)用的函數(shù)中修改變量的值,則修改后的結(jié)果對(duì)外部函數(shù)也是有影響的,例如:
#!/bin/bash update_prefecture1() { echo "[update_prefecture1] $prefecture" prefecture=Aomori echo "[update_prefecture1] $prefecture" } update_prefecture2() { local prefecture=Iwate echo "[update_prefecture2] $prefecture" update_prefecture1 echo "[update_prefecture2] $prefecture" } prefecture=Hokkaido echo "$prefecture" update_prefecture2 echo "$prefecture" $ ./local_param_nest.sh Hokkaido [update_prefecture2] Iwate [update_prefecture1] Iwate ←----- 內(nèi)部函數(shù)沿用在外部函數(shù)中定義的變量的值 [update_prefecture1] Aomori ←----- 在內(nèi)部函數(shù)中修改變量值 [update_prefecture2] Aomori ←----- 外部函數(shù)的值也被修改了 Hokkaido ←----- 函數(shù)外部的全局變量不受影響
local命令可以使用和declare命令相同的參數(shù)選,例如定義數(shù)組類型的局部變量
local-aprefectures=(IwateMiyagi)
要想在函數(shù)內(nèi)部使用指定的參數(shù),可以使用位置參數(shù)$1, $2
#!/bin/bash print_argument() { echo "$0 = $0" echo "$1 = $1" echo "$2 = $2" echo "$3 = $3" echo "$# = $#" } print_argument aaa bbb ←----- 在調(diào)用函數(shù)時(shí)傳遞參數(shù)
FUNCNAME是bash的一個(gè)數(shù)組類型的變量,里面保存了當(dāng)前運(yùn)行的所有函數(shù)名,通過(guò)變量FUNCNAME獲取并輸出函數(shù)名
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4346瀏覽量
62978 -
Shell腳本
+關(guān)注
關(guān)注
0文章
36瀏覽量
8023
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
【RT-Thread學(xué)習(xí)筆記】結(jié)合案例學(xué)習(xí)調(diào)試shell腳本
![【RT-Thread學(xué)習(xí)筆記】結(jié)合案例學(xué)習(xí)調(diào)試<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>](https://file.elecfans.com//web2/M00/57/85/poYBAGLic7mADH7jAAC4VCToDMQ189.jpg)
嵌入式和物聯(lián)網(wǎng)的shell腳本學(xué)習(xí)指南之shell腳本入門免費(fèi)下載
![嵌入式和物聯(lián)網(wǎng)的<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>學(xué)習(xí)指南之<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>入門免費(fèi)下載](https://file.elecfans.com/web1/M00/63/76/o4YBAFuXkYCALlMqAAHAKzTEIaQ501.png)
shell腳本最簡(jiǎn)明的教程
![<b class='flag-5'>shell</b><b class='flag-5'>腳本</b>最簡(jiǎn)明的教程](https://file.elecfans.com/web1/M00/68/8E/pIYBAFvFWqCAWecrAAARSOanVz0569.png)
109個(gè)實(shí)用shell腳本分享
shell腳本基礎(chǔ)知識(shí)
剖析Linux shell 函數(shù)實(shí)現(xiàn)
![剖析Linux <b class='flag-5'>shell</b> <b class='flag-5'>函數(shù)</b>實(shí)現(xiàn)](https://file1.elecfans.com//web2/M00/82/B2/wKgZomRdpASAAk5xAABOA2lJ49o392.jpg)
Linux Shell腳本經(jīng)典案例分享
![Linux <b class='flag-5'>Shell</b><b class='flag-5'>腳本</b>經(jīng)典案例分享](https://file1.elecfans.com/web2/M00/89/DD/wKgZomSL-7eAcRBVAAA0YQQcU5I277.png)
BASH shell腳本篇—函數(shù)
Shell腳本檢查工具ShellCheck介紹
![<b class='flag-5'>Shell</b><b class='flag-5'>腳本</b>檢查工具ShellCheck介紹](https://file1.elecfans.com/web2/M00/B9/8A/wKgaomWLucKAJ0XtAAAT6zsU20o727.png)
Linux從零到精通:最簡(jiǎn)單的Shell腳本入門教程
![Linux從零到精通:最簡(jiǎn)單的<b class='flag-5'>Shell</b><b class='flag-5'>腳本</b>入門教程](https://file1.elecfans.com/web3/M00/01/1E/wKgZPGdRCS2ACku7AAAiM7-x7u0394.png)
評(píng)論