知識體系:
#創建函數
#使用參數
#共享函數
shell腳本代碼可以封裝在函數內,而函數可以在腳本任意處使用,由此減少大段代碼的重複編寫.
1、基本腳本函數
函數是被賦予名稱的腳本代碼塊,可以在代碼的任意位置使用.當需要調用代碼塊的時候,只需引用代碼塊被賦予的函數名即可.
1.1、創建函數
在bash shell腳本中創建函數可以使用兩種方式:
*關鍵字function,基本格式為:
function name{
commands
}
*()方式:
name() {
commands
}
下面就對這兩種方式舉例創建.
1.2、使用函數
如下採用上面創建函數的兩種方式舉例:
[root@wzp ~]# chmod u x 7.1test
[root@wzp ~]# cat 7.1test
#!/bin/bash
function fun1 {
echo "this is the first function"
}
fun1
fun2() {
echo "this is the second function"
}
fun2
[root@wzp ~]# ./7.1test
this is the first function
this is the second function
這裡頭有兩個函數名:fun1和fun2,對應兩個函數執行后顯示不同結果.
但是有點要注意的是必須先定義好函數,后才能調用,看個例子先:
[root@wzp ~]# cat 7.1test
#!/bin/bash
function fun1 {
echo "this is the first function"
}
fun1
fun2
fun2() {
echo "this is the second function"
}
[root@wzp ~]# ./7.1test
this is the first function
./7.1test: line 6: fun2: command not found
如上我把fun2放置在定義函數fun2之前就調用,shell是不會回頭來執行的,對於函數fun2就報錯了.
2、返回值
bash shell將函數看成小型腳本,並以退出狀態結束,我們可以通過$?查看三種不同的退出狀態,這一點前面已經講述過了.
2.1、默認退出狀態
我們先來看一個例子:
[root@wzp ~]# cat 7.2test
#!/bin/bash
function fun1 {
echo "this is the first function"
ls -l xx
}
fun1
echo "the exit status is :$?"
[root@wzp ~]# chmod u x 7.2test
[root@wzp ~]# ./7.2test
this is the first function
ls: xx: 沒有那個文件或目錄
the exit status is :2
我們看到函數的退出狀態為2,一條命令執行出錯了.再來看看另外一種情況:
[root@wzp ~]# cat 7.2test
#!/bin/bash
function fun1 {
ls -l xx
echo "this is the first function"
}
fun1
echo "the exit status is :$?"
[root@wzp ~]# ./7.2test
ls: xx: 沒有那個文件或目錄
this is the first function
the exit status is :0
我把函數的兩條命令調換位置執行,可以看到第一條報錯了,然後退出狀態值卻是0.從這點我們發現函數的默認退出狀態是不可靠的.
3、在函數中使用變數
如下講述shell腳本函數內外變數處理方法.
3.1、向函數傳遞參數
bash shell將函數作為小型腳本處理,函數可以使用標準參數環境變數來表示命令行傳遞給函數的參數,先看如下例子:
[root@wzp ~]# cat 7.3test
#!/bin/bash
function cio {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 $1 ]
else
echo $[ $1 * $2 ]
4、創建庫
函數在單個腳本中有助於減少代碼量,如果多個腳本碰巧使用相同的函數可以通過創建函數的庫文件,然後再不同腳本中引用該庫文件.這種方法,要創建公共庫文件,如下:
[root@wzp ~]# cat funcs
function ceo {
echo $[ $1 $2 ]
}
function cfo {
echo $[ $1 * $2 ]
}
接下來把如上庫文件調用進腳本文件.然後問題在於如果把這個庫文件作為普通腳本文件運行,那麼這些函數不會出現在腳本文件中,看如下例:
[root@wzp ~]# chmod u x funcs
[root@wzp ~]# chmod u x 7.41test
[root@wzp ~]# chmod u x 7.42test
[root@wzp ~]# cat 7.41test
#!/bin/bash
./funcs
result=`ceo 12 24`
echo "the result is $result"
[root@wzp ~]# ./7.41test
./7.41test: line 3: ceo: command not found
the result is
可以看到調用庫文件失敗了,問題就處在於通過./執行庫函數是不可行的.
解決辦法是通過點符號符在shell腳本中運行庫文件腳本,即為source命令,其格式是:
. ./xx 把例子修改如下:
[root@wzp ~]# cat 7.41test
#!/bin/bash
. ./funcs
result=`ceo 12 24`
echo "the result is $result"
[root@wzp ~]# cat 7.42test
#!/bin/bash
. ./funcs
result=`cfo 12 5`
echo "the result is $result"
[root@wzp ~]# ./7.41test
the result is 36
[root@wzp ~]# ./7.42test
the result is 60
我們看到了滿意的結果,關鍵在於通過. ./點符號符來是的腳本能以調用庫文件的函數,這裡頭是把腳本文件和庫文件放置於同一目錄,為了確保路徑問題的可靠性可使用絕對路徑.
5、在命令行使用函數
有時候想直接在命令行下使用函數也是可以的,在命令行下使用函數有兩種方法,分別如下:
1、將函數定義在一行命令中:
[root@wzp ~]# function 51cto { echo $[ $1 / $2 ]; }
[root@wzp ~]# 51cto 100 20
5
上面這種方法必須在命令的結尾處加分號,是的shell知道命令在哪裡分開.
2、使用多行命令定義函數:
[root@wzp ~]# function 51ceo {
> echo $[ $1 * $2 ]
> }
[root@wzp ~]# 51ceo 3 8
24
通過次提示符輸入更多命令,在函數末尾處使用大括弧,shell就知道了定義函數結束,這種方法就不用使用分號了.
本文出自 「twenty_four」 博客,請務必保留此出處http://twentyfour.blog.51cto.com/945260/525126
[火星人 ] bash shell筆記7 創建函數已經有1082次圍觀