寫在前面:案例、常用、歸類、解釋說明。(By Jim)
使用函數
#!/bin/bash # testing the script function myfun { echo "This is an example of a function" } count=1 while [ $count -le 5 ] do myfun count=$[ $count +1 ] done echo "This is the end of the loop" myfun echo "Now this is the end of the script"
(記得空格,函數一定要在使用之前定義,函數名必須唯一)
返回值
可以通過$?來確定函數的退出狀態
使用return命令
#!/bin/bash # testing the script function myfun { read -p "Enter a value:" value echo "double the value" return $[ $value * 2 ] } myfun echo "The new vlue is $?"
結果:
Enter a value:23
double the value
The new vlue is 46
(退出狀態的取值范圍是0到255,$?是最近已執行命令的退出狀態)
使用函數輸出,這種方法最靠譜了
看例子
#!/bin/bash # testing the script function myfun { read -p "Enter a value:" value echo $[ $value * 2 ] } result=`myfun` echo "The new vlue is $result"
(這樣就能很好的利用輸出結果了)
在函數中使用變量
向函數傳遞參數
#!/bin/bash # testing the script function 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 15:" value=`addem 10 15` echo $value echo -n "Just one number 10:" value=`addem 10` echo $value echo -n "No numbers:" value=`addem` echo $value echo -n "More than two numbers:" vaule=`addem 10 15 20` echo $value
結果:
Adding 10 and 15:25
Just one number 10:20
No numbers:-1
(由上述可知,$#可以得到參數的個數,$1表示第一個參數,$2表示第二個參數)
#!/bin/bash # testing the script function addem { if [ $# -eq 0 ]||[ $# -gt 2 ] then echo -1 elif [ $# -eq 1 ] then echo $[ $1 + $1 ] else echo $[ $1 + $2 ] fi } if [ $# -eq 2 ] then value=`addem $1 $2` echo "The value is $value" else echo "Usage:test1 a b" fi
(函數外面的$#表示調用腳本使用的參數數,函數里的$#表示調用函數使用的參數數,注意這點區別)
作用域是變量的可見區域。
全局變量,在shell腳本內處處有效的變量。函數中定義全局變量,那么主代碼中有效。主代碼中定義全局,函數中也有效。
默認情況下,腳本中定義的變量都是全局變量。
看例子:
#!/bin/bash # testing the script function myfun { value=$[ $value * 2 ] } read -p "Enter a value:" value myfun echo "The new value is:$value"
(輸入45,得到90,這里的value在函數中發生變化了,到腳本中一樣可以使用,而且已經變化了)
局部變量
作用范圍只在函數當中
關鍵字local
#!/bin/bash # testing the script function myfun { local value=$[ $value * 2 ] } read -p "Enter a value:" value myfun echo "The new value is:$value"
(輸入45,輸出45。因為加上local關鍵字之后,函數中的value是局部變量,與外界無關)
#!/bin/bash # testing the script function myfun { local value=$[ $value * 2 ] echo $value } read -p "Enter a value:" value result=`myfun` echo "The new value is:$value" echo "The result of the fun is $result"
(不過可以通過輸出來獲取函數處理的結果,這里的result還是處理后的結果,比如輸入45,處理后是90)
數組變量與函數
#!/bin/bash # testing the script function addarray { local sum=0 local newarray newarray=(`echo "$@"`) for value in ${newarray[*]} do sum=$[ $sum + $value ] done echo $sum } myarray=(1 2 3 4 5) echo "The original array is :${myarray[*]}" arg=`echo ${myarray[*]}` result=`addarray $arg` echo "The result is $result"
結果:
The original array is :1 2 3 4 5
The result is 15
(數組參數傳入函數,函數獲取后,進行處理輸出。腳本將輸出結果打印)
#!/bin/bash # testing the script function addarray { local sum=0 local newarray newarray=(`echo "$@"`) for value in ${newarray[*]} do sum=$[ $sum + $value ] done echo ${newarray[*]} } myarray=(1 2 3 4 5) echo "The original array is :${myarray[*]}" arg=`echo ${myarray[*]}` result=`addarray $arg` echo "The result is $result"
(輸出數組)
函數遞歸
#!/bin/bash # testing the script function factorial { if [ $1 -eq 1 ] then echo 1 else local temp=$[ $1 -1 ] local result=`factorial $temp` echo $[ $result * $1 ] fi } read -p "Enter value:" value result=`factorial $value` echo "The factorial of $value is:$result"
(輸入5,輸出120。大環套小環,小環套更小,逐步執行。
我們一步一步剖析
5輸入得到5*4!
4又得到4*3!
3又得到3*2!
2又得到2*1
由此得到5*4*3*2*1也就是120
)
顯然單個腳本有助於減少輸入量,但是如果多個腳本碰巧使用同樣的函數又該怎樣?
創建庫
#my script functions function addem { echo $[ $1 + $2 ] } function multem { echo $[ $1 * $2 ] } function divem { if [ $2 -ne 0 ] then echo $[ $1 / $2 ] else echo -1 fi }
使用函數庫的關鍵詞是source
#!/bin/bash # testing the script source ./myfuncs result=`addem 10 15` echo "The result is $result"
(通過source就能使用庫函數了)
#!/bin/bash
# testing the script
. ./myfuncs
result=`addem 10 15`
echo "The result is $result"
(或者點操作符,效果是一樣的)
在命令行中使用函數
[root@localhost shellscript]# function multem {
> echo $[ $1 * $2 ]
> }
[root@localhost shellscript]# multem 2 5
10
(多行命令)
在.bashrc文件中定義函數
直接在命令行定義shell函數的缺點是一旦退出shell,函數定義將失效。
比較省事的方法是將函數定義在shell每次啟動都能重新載入的地方。.bashrc文件就是這樣的地方。
可以用source(點操作符)將現有庫文件的函數包含進.bashrc腳本。
重新啟動命令(source .bashrc)
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
function addem {
echo $[ $1 + $2 ]
}
(重啟之后,新加入的函數就有效了)
腳本中也可以使用了