linux學習之shell指令碼 ------- shell函數

來源:互聯網
上載者:User

標籤:shell函數   函數定義   函數調用   函數檔案   

[本文是自己學習所做筆記,歡迎轉載,但請註明出處:http://blog.csdn.net/jesson20121020]

  今天來看一下Shell的函數。

Shell函數  函數定義

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

   函數定義格式:

    函數名(){    命令1     ……                 }   或    function 函數名(){     ……    }

   函數可以放在同一個檔案中作為一段代碼,也可以放在只包含函數的單獨檔案中。

   如下面的指令碼就是一個只包含函數的單獨檔案。

#!/bin/bash#fun_testfunction hello(){   echo "Hello,today is `date`"return 1}

   既然定義的函數,那麼如何調用該函數呢,下面,我們接著看函數調用。

  函數調用

   以上面定義的函數為例,調用方式如下:

#!/bin/bash#fun_testfunction hello(){   echo "Hello,today is `date`"return 1}echo "now going the function hello"helloecho "back from the function"
   可以看出,其實和其他語言一樣,在主程式中直接用函數名,就可以調用定義好的函數了,我們看下,該指令碼的執行結果:

[email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx fun_test.sh [email protected]:~/develop/worksapce/shell_workspace$ ./fun_test.sh now going the function helloHello,today is 2015年 02月 01日 星期日 20:22:36 CSTback from the function

  參數傳遞

   向函數傳遞參數就像是在指令碼中使用位置變數一樣,$1,...,$9

   我們修改上面的例子:

#!/bin/bash#fun_testfunction hello(){   echo "Hello,$1 today is `date`"return 1}echo "now going the function hello"hello jessonecho "back from the function"
   這裡,是以位置變數使用參數,執行如下:

[email protected]:~/develop/worksapce/shell_workspace$ ./fun_test.sh now going the function helloHello,jesson today is 2015年 02月 01日 星期日 20:28:38 CSTback from the function

  函數檔案

   在上面函數定義裡也提到了,函數可以單獨作為一個檔案,那這個檔案即稱為函數檔案,那麼就有一個問題了,上面的例子,都是在同一個檔案內,也就是函數與函數的調用都在同一個主程式中,那麼對於函數檔案,該如何調用呢。

   還是一樣,我們以例子來說明,先定義一個函數檔案,修改上個指令碼為函數檔案,如下

fun_test.sh

#!/bin/bash#fun_testfunction hello(){   echo "Hello,$1 today is `date`"return 1}
   這裡,我們定義了函數檔案,我們現在要在另一個指令碼裡調用該函數檔案,該如何做呢????

funfilecall.sh

 #!/bin/bash#funfilecall#Source function. fun_test.shecho "now going to the function hello"hello jessonecho "back from the function"
   可以看出,對於函數檔案的調用,只需要兩步就可實現,第一步就是在調用函數前,先申明函數檔案,方法( . 函數檔案名稱),這裡要注意,.與函數檔案名稱之間要有空格。接下下,就可以直接調用函數檔案裡定義的函數了,與之前在同一個檔案中調用函數一樣。該指令碼執行結果如下:

[email protected]:~/develop/worksapce/shell_workspace$ ./funfilecall.sh now going to the function helloHello,jesson today is 2015年 02月 01日 星期日 20:37:55 CSTback from the function

  檢查載入函數和刪除函數

   查看載入函數:

     set

   刪除函數

    unset

   我們修改上面的指令碼如下:

#!/bin/bash#funfilecall#Source function. fun_test.shsetunset helloecho "now going to the function hello"hello jessonecho "back from the function"
   執行結果如下:


......now going to the function hello./funfilecall.sh: 行 8: hello: 未找到命令back from the function
   可以,在指令碼中有unset hello,這句的意思是將hello刪除,也就是不載入,所以執行結果中才會提示,hello:未找到命令。

   另外,$?可以取得一個命令的返回值,一般情況,當命令執行成功時,返回0,執行不成功時返回非0整數。對於函數也類似,可以通過$?取得函數的返回值。

linux學習之shell指令碼 ------- shell函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.