Shell 函數(shù)參數(shù)
在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命令之?dāng)?shù)組表示語法學(xué)習(xí)
這篇文章主要為大家介紹了Shell命令之?dāng)?shù)組表示語法學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Shell腳本處理浮點(diǎn)數(shù)的運(yùn)算和比較實(shí)例
這篇文章主要介紹了Shell腳本處理浮點(diǎn)數(shù)的運(yùn)算和比較實(shí)例,文中分別使用了bc或awk實(shí)現(xiàn),需要的朋友可以參考下2014-06-06
實(shí)現(xiàn)釋放CentOS系統(tǒng)內(nèi)存的Shell腳本分享
這篇文章主要介紹了實(shí)現(xiàn)釋放CentOS系統(tǒng)內(nèi)存的Shell腳本分享,本文對(duì)一些小內(nèi)存的VPS特別有用,需要的朋友可以參考下2014-12-12
shell腳本操作mysql數(shù)據(jù)庫刪除重復(fù)的數(shù)據(jù)
今天小編就為大家分享一篇關(guān)于shell腳本操作mysql數(shù)據(jù)庫刪除重復(fù)的數(shù)據(jù),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
查詢上次Ubuntu重啟時(shí)間的方法命令總結(jié)
在大多數(shù)情況下,Linux 系統(tǒng)的關(guān)機(jī)時(shí)間、重啟日期和運(yùn)行時(shí)長(zhǎng)等調(diào)試信息在系統(tǒng)故障排錯(cuò)時(shí)會(huì)顯得比較重要,本文將詳細(xì)介紹多種方法來查詢上次 Ubuntu 重啟的時(shí)間,并解釋每種方法的背后原理,需要的朋友可以參考下2024-05-05
Impala-shell命令參數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Impala-shell命令參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Linux shell select菜單選擇實(shí)現(xiàn)代碼
主要介紹了Bash Shell中的select命令簡(jiǎn)單使用示例,通常用于流程控制功能的實(shí)現(xiàn),需要的朋友可以參考下2021-07-07

