如何正確的使用Shell函數(shù)-創(chuàng)新互聯(lián)

如何正確的使用Shell函數(shù)?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計,成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),網(wǎng)站設(shè)計,企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為千余家服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!

1. 在shell文件內(nèi)部定義函數(shù)并引用:

[~/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命令手動指定返回值:

代碼如下:

[~/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時會出錯。

代碼如下:

[~/shell/function]# ./return.sh
enter a: 200
print 2a: return value 144


3.函數(shù)輸出

為了返回大于255的數(shù)、浮點數(shù)和字符串值,好用函數(shù)輸出到變量:

代碼如下:

[~/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ù)):

代碼如下:

[~/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ù)部分擁有名字相同的變量時,可能會相互影響,例如:


代碼如下:

[~/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)部好使用局部變量,消除影響。

代碼如下:

[~/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ù)組變量:

代碼如下:

[~/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ù)返回數(shù)組變量

代碼如下:

[~/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


看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

網(wǎng)站題目:如何正確的使用Shell函數(shù)-創(chuàng)新互聯(lián)
本文地址:http://bm7419.com/article48/dpdghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計用戶體驗、網(wǎng)站收錄、網(wǎng)頁設(shè)計公司、外貿(mào)建站、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計