欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年07月19日 12:00:01   投稿:junjie  
這篇文章主要介紹了Shell腳本函數(shù)定義和函數(shù)參數(shù),分別介紹了2種自定義函數(shù)的方法,以及定義帶返回值函數(shù)的方法,需要的朋友可以參考下

一、Shell函數(shù)

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

shell允許將一組命令集或語句形成一個(gè)可用塊,這些塊稱為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語句;如果不加,則將最后一條命令運(yùn)行結(jié)果作為返回值(一般為0,如果執(zhí)行失敗則返回錯(cuò)誤代碼)。 return后跟數(shù)值(0-255)。

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

下面的例子定義了一個(gè)函數(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!

下面定義一個(gè)帶有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)它時(shí),才可以使用。調(diào)用函數(shù)僅使用其函數(shù)名即可。

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

在Shell中,調(diào)用函數(shù)時(shí)可以向其傳遞參數(shù)。在函數(shù)體內(nèi)部,通過 $n 的形式來獲取參數(shù)的值,例如,$1表示第一個(gè)參數(shù),$2表示第二個(gè)參數(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 不能獲取第十個(gè)參數(shù),獲取第十個(gè)參數(shù)需要${10}。當(dāng)n>=10時(shí),需要使用${n}來獲取參數(shù)。

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

相關(guān)文章

  • linux下shell腳本備份文件的方法實(shí)現(xiàn)

    linux下shell腳本備份文件的方法實(shí)現(xiàn)

    本文主要介紹了linux下shell腳本備份文件的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Linux Shell中判斷進(jìn)程是否存在的代碼

    Linux Shell中判斷進(jìn)程是否存在的代碼

    有時(shí)候我們需要在linux中判斷進(jìn)程是否存在,然后再執(zhí)行相應(yīng)的操作,這里簡(jiǎn)單的分享下,方便需要的朋友
    2013-01-01
  • Shell動(dòng)態(tài)生成數(shù)組的多種方法

    Shell動(dòng)態(tài)生成數(shù)組的多種方法

    這篇文章主要介紹了Shell動(dòng)態(tài)生成數(shù)組的多種方法,本文給出while循環(huán)法和for循環(huán)法,以及使用seq生成數(shù)組法,需要的朋友可以參考下
    2015-07-07
  • linux的cut命令用法總結(jié)

    linux的cut命令用法總結(jié)

    今天小編就為大家分享一篇關(guān)于linux的cut命令用法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • linux設(shè)置定時(shí)任務(wù)的方法步驟

    linux設(shè)置定時(shí)任務(wù)的方法步驟

    這篇文章主要介紹了linux設(shè)置定時(shí)任務(wù)的方法步驟,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • git 刪除分支和回滾的實(shí)例詳解

    git 刪除分支和回滾的實(shí)例詳解

    這篇文章主要介紹了git 刪除分支和回滾的實(shí)例詳解的相關(guān)資料,希望通過本文大家能理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • nvidia-smi命令詳解和一些高階技巧講解

    nvidia-smi命令詳解和一些高階技巧講解

    一般情況下用的比較多的就是nvidia-smi的命令,其實(shí)掌握了這一個(gè)命令也就能夠覆蓋絕大多數(shù)場(chǎng)景了,但是本質(zhì)求真務(wù)實(shí)的態(tài)度,本文調(diào)研了相關(guān)資料,整理了一些比較常用的nvidia-smi命令的其他用法,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • Shell中處理包含空格的文件名實(shí)例

    Shell中處理包含空格的文件名實(shí)例

    這篇文章主要介紹了Shell中處理包含空格的文件名實(shí)例,需要的朋友可以參考下
    2014-05-05
  • linux?shell輸出換行簡(jiǎn)單實(shí)例

    linux?shell輸出換行簡(jiǎn)單實(shí)例

    這篇文章主要給大家介紹了關(guān)于linux?shell輸出換行的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-02-02
  • linux 守護(hù)進(jìn)程詳解及建立守護(hù)進(jìn)程

    linux 守護(hù)進(jìn)程詳解及建立守護(hù)進(jìn)程

    這篇文章主要介紹了linux 守護(hù)進(jìn)程詳解及建立守護(hù)進(jìn)程的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評(píng)論