這篇文章主要介紹“如何理解Shell函數(shù)參數(shù)”,在日常操作中,相信很多人在如何理解Shell函數(shù)參數(shù)問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何理解Shell函數(shù)參數(shù)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
在shell中,調(diào)用函數(shù)時(shí)可以向其傳遞參數(shù)。在函數(shù)體內(nèi)部,通過 $n 的形式來獲取參數(shù)的值,例如,$1表示第一個(gè)參數(shù),$2表示第二個(gè)參數(shù)...
帶參數(shù)的函數(shù)示例:
#!/bin/bash funWithParam(){ echo "The value of the first parameter is $1 !" echo "The value of the second parameter is $2 !" echo "The value of the tenth parameter is $10 !" echo "The value of the tenth parameter is ${10} !" echo "The value of the eleventh parameter is ${11} !" echo "The amount of the parameters is $# !" echo "The string of the parameters is $* !" } funWithParam 1 2 3 4 5 6 7 8 9 34 73
輸出:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"
注意,$10 不能獲取第十個(gè)參數(shù),獲取第十個(gè)參數(shù)需要${10}。當(dāng)n>=10時(shí),需要使用${n}來獲取參數(shù)。
另外,還有幾個(gè)特殊字符用來處理參數(shù):
參數(shù)處理 | 說明 |
---|---|
$# | 傳遞到腳本的參數(shù)個(gè)數(shù) |
$* | 以一個(gè)單字符串顯示所有向腳本傳遞的參數(shù) |
$$ | 腳本運(yùn)行的當(dāng)前進(jìn)程ID號(hào) |
$! | 后臺(tái)運(yùn)行的最后一個(gè)進(jìn)程的ID號(hào) |
$@ | 與$#相同,但是使用時(shí)加引號(hào),并在引號(hào)中返回每個(gè)參數(shù)。 |
$- | 顯示Shell使用的當(dāng)前選項(xiàng),與set命令功能相同。 |
$? | 顯示最后命令的退出狀態(tài)。0表示沒有錯(cuò)誤,其他任何值表明有錯(cuò)誤。 |
到此,關(guān)于“如何理解Shell函數(shù)參數(shù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享名稱:如何理解Shell函數(shù)參數(shù)-創(chuàng)新互聯(lián)
標(biāo)題來源:http://www.sd-ha.com/article24/dsdije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)建站、App設(shè)計(jì)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容