濮阳杆衣贸易有限公司

主頁 > 知識庫 > Shell腳本函數(shù)定義和函數(shù)參數(shù)

Shell腳本函數(shù)定義和函數(shù)參數(shù)

熱門標(biāo)簽:陜西電銷卡外呼系統(tǒng)怎么安裝 杭州機器人外呼系統(tǒng) excel地址地圖標(biāo)注 東莞電銷機器人價格一覽表 旅游地圖標(biāo)注大全 百度地圖標(biāo)注圖標(biāo)更換 百度地圖的地圖標(biāo)注 地圖標(biāo)注超出范圍怎么辦 佛山高德地圖標(biāo)注中心

一、Shell函數(shù)

本教程目前為止所有腳本都是從頭到尾執(zhí)行。這樣做很好,但你也許已經(jīng)注意到有些腳本段間互相重復(fù)。

shell允許將一組命令集或語句形成一個可用塊,這些塊稱為shell函數(shù)。

shell中函數(shù)的定義格式如下:

復(fù)制代碼 代碼如下:
函數(shù)名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

如果愿意,可在函數(shù)名前加上關(guān)鍵字function,這取決于使用者。

復(fù)制代碼 代碼如下:
function 函數(shù)名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

函數(shù)返回值,可以顯示增加return語句;如果不加,則將最后一條命令運行結(jié)果作為返回值(一般為0,如果執(zhí)行失敗則返回錯誤代碼)。 return后跟數(shù)值(0-255)。

函數(shù)可以放在同一個文件中作為一段代碼,也可以放在只包含函數(shù)的單獨文件中。函數(shù)不必包含很多語句或命令,甚至可以只包含一個echo語句,這取決于使用者。

下面的例子定義了一個函數(shù)并進(jìn)行調(diào)用:

復(fù)制代碼 代碼如下:

#!/bin/bash
demoFun(){
    echo "This is your first shell function!"
}
echo "Function begin..."
hello
echo "Function end!"

輸出:
Function begin...
This is your first shell function!
Function end!

下面定義一個帶有return語句的函數(shù):

復(fù)制代碼 代碼如下:

#!/bin/bash
funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of two numbers is $? !"

輸出類似下面:
The function is to get the sum of two numbers...
Input first number: 25
Input another number: 50
The two numbers are 25 and 50 !
The sum of two numbers is 75 !

函數(shù)返回值在調(diào)用該函數(shù)后通過 $? 來獲得。

注意:所有函數(shù)在使用前必須定義。這意味著必須將函數(shù)放在腳本開始部分,直至shell解釋器首次發(fā)現(xiàn)它時,才可以使用。調(diào)用函數(shù)僅使用其函數(shù)名即可。

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

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

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

復(fù)制代碼 代碼如下:

#!/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}。當(dāng)n>=10時,需要使用${n}來獲取參數(shù)。

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

您可能感興趣的文章:
  • Shell動態(tài)生成數(shù)組的多種方法
  • shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)

標(biāo)簽:通遼 南充 雅安 朝陽 延邊 西藏 青島 隨州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell腳本函數(shù)定義和函數(shù)參數(shù)》,本文關(guān)鍵詞  Shell,腳本,函數(shù),定,義和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Shell腳本函數(shù)定義和函數(shù)參數(shù)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Shell腳本函數(shù)定義和函數(shù)參數(shù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    高邑县| 海丰县| 大理市| 开化县| 乌兰察布市| 武汉市| 久治县| 本溪市| 麦盖提县| 大竹县| 太湖县| 垫江县| 顺义区| 平江县| 苗栗县| 池州市| 靖边县| 南平市| 光山县| 普格县| 建水县| 化德县| 巩义市| 平原县| 耒阳市| 钟祥市| 济源市| 涟水县| 饶阳县| 通海县| 邹城市| 盐城市| 渝中区| 定南县| 海宁市| 普兰县| 萝北县| 郁南县| 东丽区| 石狮市| 德昌县|