Shell 函數(shù)參數(shù)
在shell中,調(diào)用函數(shù)時可以向其傳遞參數(shù)。在函數(shù)體內(nèi)部,通過 $n 的形式來獲取參數(shù)的值,例如,$1表示第一個參數(shù),$2表示第二個參數(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 不能獲取第十個參數(shù),獲取第十個參數(shù)需要${10}。當n>=10時,需要使用${n}來獲取參數(shù)。
另外,還有幾個特殊字符用來處理參數(shù):
| 參數(shù)處理 | 說明 |
|---|---|
| $# | 傳遞到腳本的參數(shù)個數(shù) |
| $* | 以一個單字符串顯示所有向腳本傳遞的參數(shù) |
| $$ | 腳本運行的當前進程ID號 |
| $! | 后臺運行的最后一個進程的ID號 |
| $@ | 與$#相同,但是使用時加引號,并在引號中返回每個參數(shù)。 |
| $- | 顯示Shell使用的當前選項,與set命令功能相同。 |
| $? | 顯示最后命令的退出狀態(tài)。0表示沒有錯誤,其他任何值表明有錯誤。 |
相關文章
實現(xiàn)釋放CentOS系統(tǒng)內(nèi)存的Shell腳本分享
這篇文章主要介紹了實現(xiàn)釋放CentOS系統(tǒng)內(nèi)存的Shell腳本分享,本文對一些小內(nèi)存的VPS特別有用,需要的朋友可以參考下2014-12-12
shell腳本操作mysql數(shù)據(jù)庫刪除重復的數(shù)據(jù)
今天小編就為大家分享一篇關于shell腳本操作mysql數(shù)據(jù)庫刪除重復的數(shù)據(jù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Impala-shell命令參數(shù)的實現(xiàn)
這篇文章主要介紹了Impala-shell命令參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
Linux shell select菜單選擇實現(xiàn)代碼
主要介紹了Bash Shell中的select命令簡單使用示例,通常用于流程控制功能的實現(xiàn),需要的朋友可以參考下2021-07-07

