Linux學習-進階shell指令碼編程(一)函數,shell指令碼編程

來源:互聯網
上載者:User

Linux學習-進階shell指令碼編程(一)函數,shell指令碼編程

引文: 通常編寫shell指令碼時,你會發現很多地方都要用到相同的代碼或者說是相同的功能。如果是一段小代碼,那無所謂。可如果多次使用而且還是相同的代碼,我想你也會感覺很煩的。為了能夠讓代碼重用,這就使用到函數了。

溫馨提示
變數賦值的格式為:

變數名=變數值

注意事項:

  • 變數名前面不應加美元“$”符號。(和PHP不同)等號“=”
  • 前後不可以有空格。和C語言不同,Shell中不需要顯式的文法來聲明變數。
  • 變數名不可以直接和其他字元相連,如果想相連,必須用括弧:echo “this is $(he)llo!”
函數定義格式
function name {    commands}

或者是

name() {}

這個就和其他的語言有點類似了。

執行個體1.無參函數

一個簡單的使用函數功能的shell指令碼
test1.sh

#!/bin/bash# this is a test file for test functionfunction func1 {        echo "this is an example of function!"}count=1echo "count's value is $count."while [ $count -le 5 ]do        func1        count = $[ $count + 1 ]doneecho "end of while."

運行:

sh test1.sh

輸出:
count’s value is 1.
this is an example of function!
this is an example of function!
this is an example of function!
this is an example of function!
this is an example of function!
end of while.

執行個體2.帶參函數

test2.sh

#!/bin/bash# this is a test file for test functionfunction addem {        if [ $# -eq 0 ] || [ $# -gt 2 ]        then                echo -1        elif [ $# -eq 1 ]        then                echo $[ $1 + $1]        else                echo $[ $1 + $2 ]        fi}echo -n "Adding 10 and 5:"value=`addem 10 15`echo $valueecho -n "Let's try adding just one number:"value=`addem 10`echo $valueecho -n "Now trying adding no number:"value=`addem`echo $valueecho -n "Finally,we try adding three numbers:"value=`addem 10 15 20`echo $valueecho "end of file."

測試:

sh test2.sh

輸出:
Adding 10 and 5:25
Let’s try adding just one number:20
Now trying adding no number:-1
Finally,we try adding three numbers:-1
end of file.

如果想在函數內部聲明一個變數,可以使用local來定義,表示為局部變數。如local temp=1

執行個體3.數組參數

test3.sh

#!/bin/bash# trying to pass a array variablefunction testit {        local newarray   # use ‘local’ define a local variable        newarray=(`echo "$@"`)        echo "the newarray value is ${newarray[*]}"}myarray=(1,2,3,4,5)echo "the original array is:${myarray[*]}"testit $myarray

測試:

sh test3.sh

輸出:

the original array is:1,2,3,4,5
the newarray value is 1,2,3,4,5

執行個體4.函數遞迴

定義階乘函數x! = x * (x-1)

function factorial {    if [ $1 -eq 1 ]    then        echo 1    else        local temp=$[ $1 - 1 ]        local result=`factorial $temp`        echo $[$result * $1]    fi}

詳細執行個體:test4.py

#!/bin/bash# using recursionfunction factorial {        if [ $1 -eq 1 ]        then                echo 1        else                local temp=$[ $1 - 1 ]                local result=`factorial $temp`                echo $[ $result * $1 ]        fi}

測試:

sh test4.py

輸出:
Enter value:6
The factorial of 6 is: 720

執行個體5.命令列上使用函數命令
[root@master chapter16]# function multm {> echo $[ $1 * $2 ]> }[root@master chapter16]# multm 2 510

注意: 在命令列上定義函數時,如果你給函數起的名字跟內建命令or另一個命令相同名字,則函數將覆蓋原來的命令。

如果想要開機生效,可以把函數卸載/etc/.bashrc檔案中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.