shell學習之函數與庫(一)

來源:互聯網
上載者:User

標籤:function   style   color   

函數堪稱shell的精華,有了函數和庫,shell處理複雜問題也像編譯語言那樣有頭有臉了,總之,shell的函數和庫的重要性不言而喻。

1 函數的定義

建議使用

FUNC_NAME (){}的定義方法

有些人可能喜歡在前面加上function標記,這是bash專屬的

function FUNC_NAME(){}

有點是簡潔,容易識別是函數

2 函數傳回值

一般可以用

①在函數中,return 返回

②調用函數,通過命令引用 比如 a=`func_name haha`

3 書上的一個小例子,計算1-6的二次方,三次方

[[email protected] shell]# cat square-cube.sh

#!/bin/bash

#

txt="_mktmp"

square_cube()

{

echo "$2 * $2"|bc > $1

echo "$2 * $2 * $2"|bc >> $1

}

for i in `seq 1 5`

do

square_cube $txt $i

square=`head -1 $txt`

cube=`tail -1 $txt`

echo "square of $i is $square"

echo "cube of $i is $cube"

done

rm -f $txt

執行結果:

[[email protected] shell]# ./square-cube.sh

square of 1 is 1

cube of 1 is 1

square of 2 is 4

cube of 2 is 8

square of 3 is 9

cube of 3 is 27

square of 4 is 16

cube of 4 is 64

square of 5 is 25

cube of 5 is 125

總結下:這裡是一個很簡單的函數調用,函數的功能就是處理辦理計算結果儲存在特定位置,主指令碼負責控制流程程

tips:1和l很相似,大家要注意,害的我浪費了時間

4 debugger指令碼

[[email protected] shell]# cat debugger.sh

#!/bin/bash

#

#this a debugger by function

LOGFILE=/tmp/log.log

#指定指令碼名

APPNAME=$0

#指定DEBUG層級,越高就顯示越多的冗餘資訊

VERBOSE=10

#logmsg負責顯示傳遞給它的訊息

logmsg()

{

echo "$APPNAME:`date`[email protected]" >> $LOGFILE

}

#debug負責顯示錯誤/調試 資訊

debug()

{

verbosity=$1

#shift的目的在於把層級挪走,以便只顯示debug資訊

shift

if [ "$verbosity" -le "$VERBOSE" ] ;then

echo "$APPNAME:`date`---level:${verbosity}---:[email protected]" >> $LOGFILE

fi

}

#die函數你懂得

die(){

echo "$APPNAME:`date`----fatal [email protected]"

exit 1

}

#主指令碼測試

#顯示指令碼開始,好像有點多餘哈

logmsg now the log-checking system start -------

#測試uname -a命令是否存在

uname -a||die uname -a command not find.

#uname存在的話,繼續執行

logmsg -----------system-info`uname -a`

#判斷是redhat或者debian系統

cat /etc/redhat-release||debug 8 this is not a redhat system!

cat /etc/debian-release||debug 8 this is not a debian system!

#書上更多點,我這裡省略了一些,為了簡潔

運行結果

[[email protected] shell]# ./debugger.sh

Linux www.centos.vbird 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

CentOS release 6.4 (Final)

cat: /etc/debian-release: No such file or directory

總結下,這裡三個函數互相協調使用,有撤銷的,有報錯的,注意[email protected]指全部參數,$0指檔案名稱,$1指第一個參數,等,這些如果不知道的話得惡補啦

5 遞迴函式舉例

典型a:計算階乘

[[email protected] shell]# cat factorial.sh

#!/bin/bash

#

factorial(){

if [ "$1" -gt "1" ];then

previous=`expr $1 - 1`

parent=`factorial $previous`

result=`expr $1 \* $parent`

echo $result

else

echo 1

fi

}

factorial $1

執行結果:

[[email protected] shell]# sh -x factorial.sh 3

+ factorial 3

+ ‘[‘ 3 -gt 1 ‘]‘ ;;判斷是否是1

++ expr 3 - 1

+ previous=2

++ factorial 2 ;;由於parent=`factorial $previous`在函數中調用了該函數,所以是遞迴

++ ‘[‘ 2 -gt 1 ‘]‘

+++ expr 2 - 1

++ previous=1

+++ factorial 1

+++ ‘[‘ 1 -gt 1 ‘]‘

+++ echo 1

++ parent=1 ;;最後知道初始條件為1的階乘為1

+++ expr 2 ‘*‘ 1

++ result=2

++ echo 2

+ parent=2

++ expr 3 ‘*‘ 2

+ result=6

+ echo 6

6

可以算大的數,就是慢…

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.