1. 在shell文件內(nèi)部定義函數(shù)并引用:
復(fù)制代碼 代碼如下:
[~/shell/function]# cat factorial.sh
#!/bin/bash
function factorial
{
factorial=1
for (( i=1;i = $1;i++ ))
do
factorial=$[ $factorial * $i ]
done
echo $1的階乘是:$factorial
}
echo '程序名':$0,用于求階乘
factorial $1
[~/shell/function]# ./factorial.sh 10
程序名:./factorial.sh,用于求階乘
10的階乘是:3628800
2.返回值
函數(shù)返回碼是指函數(shù)最后一條命令的狀態(tài)碼,可以用于函數(shù)返回值
使用return命令手動(dòng)指定返回值:
復(fù)制代碼 代碼如下:
[~/shell/function]# cat return.sh
#!/bin/bash
function fun1 {
read -p "enter a: " a
echo -n "print 2a: "
return $[ $a * 2 ]
}
fun1
echo "return value $?"
[~/shell/function]# ./return.sh
enter a: 100
print 2a: return value 200
由于shell狀態(tài)碼最大是255,所以當(dāng)返回值大于255時(shí)會(huì)出錯(cuò)。
復(fù)制代碼 代碼如下:
[~/shell/function]# ./return.sh
enter a: 200
print 2a: return value 144
3.函數(shù)輸出
為了返回大于255的數(shù)、浮點(diǎn)數(shù)和字符串值,最好用函數(shù)輸出到變量:
復(fù)制代碼 代碼如下:
[~/shell/function]# cat ./fun_out.sh
#!/bin/bash
function fun2 {
read -p "enter a: " a
echo -n "print 2a: "
echo $[ $a * 2 ]
}
result=`fun2`
echo "return value $result"
[~/shell/function]# ./fun_out.sh
enter a: 400
return value print 2a: 800
4.向函數(shù)傳遞參數(shù)(使用位置參數(shù)):
復(fù)制代碼 代碼如下:
[~/shell/function]# cat ./parameter.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
fun3() {
echo $[ $1 * $2 * $3 ]
}
result=`fun3 $1 $2 $3`
echo the result is $result
[~/shell/function]# ./parameter.sh 1 2 3
the result is 6
[~/shell/function]# ./parameter.sh 1 2
usage: ./parameter.sh a b c
5.全局變量與局部變量
默認(rèn)條件下,在函數(shù)和shell主體中建立的變量都是全局變量,可以相互引用,當(dāng)shell主體部分與函數(shù)部分擁有名字相同的變量時(shí),可能會(huì)相互影響,例如:
復(fù)制代碼 代碼如下:
[~/shell/function]# cat ./variable.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
temp=`echo "scale=3;$1*$2*$3" | bc -ql`
result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then
echo "temp is larger"
else
echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh 12 3 2
temp is: 5
value is: 6
the result is 72
temp is larger
在這種情況下,在函數(shù)內(nèi)部最好使用局部變量,消除影響。
復(fù)制代碼 代碼如下:
[~/shell/function]# cat ./variable.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
local temp=`echo "scale=3;$1*$2*$3" | bc -ql`
result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then
echo "temp is larger"
else
echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh 12 3 2
temp is: 5
value is: 6
the result is 72
temp is still smaller
6.向函數(shù)傳遞數(shù)組變量:
復(fù)制代碼 代碼如下:
[~/shell/function]# cat array.sh
#!/bin/bash
a=(11 12 13 14 15)
echo ${a[*]}
function array(){
echo parameters : "$@"
local factorial=1
for value in "$@"
do
factorial=$[ $factorial * $value ]
done
echo $factorial
}
array ${a[*]}
[~/shell/function]# ./array.sh
11 12 13 14 15
parameters : 11 12 13 14 15
360360
7.函數(shù)返回?cái)?shù)組變量
復(fù)制代碼 代碼如下:
[~/shell/function]# cat array1.sh
#!/bin/bash
a=(11 12 13 14 15)
function array(){
echo parameters : "$@"
local newarray=(`echo "$@"`)
local element="$#"
local i
for (( i = 0; i $element; i++ ))
{
newarray[$i]=$[ ${newarray[$i]} * 2 ]
}
echo new value:${newarray[*]}
}
result=`array ${a[*]}`
echo ${result[*]}
[~/shell/function]# ./array1.sh
parameters : 11 12 13 14 15 new value:22 24 26 28 30
您可能感興趣的文章:- Shell中關(guān)于時(shí)間和日期的函數(shù)總結(jié)
- Linux 在Shell腳本中使用函數(shù)實(shí)例詳解
- shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
- Shell使用Epoch進(jìn)行日期時(shí)間轉(zhuǎn)換和計(jì)算的幾個(gè)小函數(shù)
- Linux Shell函數(shù)返回值
- shell中函數(shù)的應(yīng)用
- PowerShell中的函數(shù)重載示例
- Shell中函數(shù)返回值超出問題
- Shell腳本中使用function(函數(shù))示例
- 淺談Shell中的函數(shù)