国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Linux Shell > 正文

Shell 函數(shù)參數(shù)

2020-07-27 19:13:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在shell中,調(diào)用函數(shù)時(shí)可以向其傳遞參數(shù)。在函數(shù)體內(nèi)部,通過(guò) $n 的形式來(lái)獲取參數(shù)的值,例如,$1表示第一個(gè)參數(shù),$2表示第二個(gè)參數(shù)...

帶參數(shù)的函數(shù)示例:

#!/bin/bashfunWithParam(){  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}來(lái)獲取參數(shù)。

另外,還有幾個(gè)特殊字符用來(lái)處理參數(shù):

參數(shù)處理 說(shuō)明
$# 傳遞到腳本的參數(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表示沒(méi)有錯(cuò)誤,其他任何值表明有錯(cuò)誤。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五寨县| 寿宁县| 开远市| 高雄市| 闸北区| 丹凤县| 汝州市| 临高县| 板桥市| 共和县| 伽师县| 鄂托克前旗| 定边县| 古田县| 呼伦贝尔市| 德州市| 澄迈县| 嘉兴市| 洛隆县| 卓资县| 汤阴县| 海门市| 内乡县| 济阳县| 平武县| 福泉市| 红河县| 罗甸县| 巴林左旗| 射洪县| 洞口县| 忻城县| 松阳县| 巴东县| 五家渠市| 汨罗市| 黄梅县| 望谟县| 阿克| 苏州市| 西青区|