麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Linux Shell > 正文

Shell函數的7種用法介紹

2020-07-27 19:12:18
字體:
來源:轉載
供稿:網友

1. 在shell文件內部定義函數并引用:

復制代碼 代碼如下:

[~/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.返回值

函數返回碼是指函數最后一條命令的狀態碼,可以用于函數返回值
使用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狀態碼最大是255,所以當返回值大于255時會出錯。

復制代碼 代碼如下:

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

3.函數輸出

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

復制代碼 代碼如下:

[~/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.向函數傳遞參數(使用位置參數):

復制代碼 代碼如下:

[~/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.全局變量與局部變量

默認條件下,在函數和shell主體中建立的變量都是全局變量,可以相互引用,當shell主體部分與函數部分擁有名字相同的變量時,可能會相互影響,例如:

復制代碼 代碼如下:

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

在這種情況下,在函數內部最好使用局部變量,消除影響。

復制代碼 代碼如下:

[~/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.向函數傳遞數組變量:

復制代碼 代碼如下:

[~/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.函數返回數組變量

復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久中精品2020中文 | 青青操国产 | 国产超碰人人爽人人做人人爱 | 成人免费网视频 | 干色视频 | 九九精品影院 | 奶子吧naiziba.cc免费午夜片在线观看 | 国产69精品久久久久久久久久 | 国产精品一区在线免费观看 | 日本精品免费观看 | 青青草国产在线视频 | 久色精品 | 一二区电影 | 日韩黄色片免费看 | 西川av在线一区二区三区 | 日韩高清影视 | 中文字幕国产日韩 | 午夜视频在线观看91 | 国产免费激情视频 | 91久久久久久久久久久久久久 | 国产精品久久久久久久久久久久午夜 | 国产一级在线免费观看 | 免费观看黄色一级视频 | 欧美成在人线a免费 | 久久久久av电影 | 欧美成人免费在线视频 | 黄色伊人网站 | 精品一区二区在线播放 | 久久久久免费电影 | 国产1区2 | 福利免费在线观看 | 国产一级做a | 日本中文字幕高清 | 美女在线观看视频一区二区 | 国产毛片网| 欧美成人小视频 | 日韩.www| va毛片 | 午夜久久视频 | 一级电影在线免费观看 | 欧洲a级片 |