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)文章
淺談安裝ORACLE時(shí)在Linux上設(shè)置內(nèi)核參數(shù)的含義
下面小編就為大家?guī)硪黄獪\談安裝ORACLE時(shí)在Linux上設(shè)置內(nèi)核參數(shù)的含義。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03shell 腳本自動(dòng)搭建nfs服務(wù)的方法示例
這篇文章主要介紹了shell 腳本自動(dòng)搭建nfs服務(wù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05shell將腳本輸出結(jié)果記錄到日志文件的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猻hell將腳本輸出結(jié)果記錄到日志文件的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Linux?ps命令詳解及Linux查看進(jìn)程的操作方法
這篇文章主要介紹了Linux?ps命令詳解,Linux查看進(jìn)程的操作方法,ps命令常用的方式有三種,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12Linux命令每天必學(xué)之 useradd/adduser 新增用戶
Linux下useradd或adduser命令用來建立用戶帳號(hào)和創(chuàng)建用戶的起始目錄,使用權(quán)限是超級(jí)用戶。接下來通過本文給大家介紹每天必學(xué)Linux命令之 useradd/adduser 新增用戶的相關(guān)知識(shí),需要的朋友參考下吧2018-10-10Linux 按時(shí)間批量刪除文件命令(刪除N天前文件)
這篇文章主要介紹了Linux 按時(shí)間批量刪除文件的命令寫法(刪除N天前文件),需要的朋友可以參考下2017-05-05