好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Shell函數的7種用法介紹

Shell函數的7種用法介紹

熱門標簽:excel地址地圖標注 百度地圖的地圖標注 百度地圖標注圖標更換 杭州機器人外呼系統 陜西電銷卡外呼系統怎么安裝 旅游地圖標注大全 佛山高德地圖標注中心 東莞電銷機器人價格一覽表 地圖標注超出范圍怎么辦

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

您可能感興趣的文章:
  • Shell中關于時間和日期的函數總結
  • Linux 在Shell腳本中使用函數實例詳解
  • shell 使用數組作為函數參數的方法(詳解)
  • Shell使用Epoch進行日期時間轉換和計算的幾個小函數
  • Linux Shell函數返回值
  • shell中函數的應用
  • PowerShell中的函數重載示例
  • Shell中函數返回值超出問題
  • Shell腳本中使用function(函數)示例
  • 淺談Shell中的函數

標簽:朝陽 通遼 青島 南充 西藏 隨州 雅安 延邊

巨人網絡通訊聲明:本文標題《Shell函數的7種用法介紹》,本文關鍵詞  Shell,函數,的,7種,用法,介紹,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Shell函數的7種用法介紹》相關的同類信息!
  • 本頁收集關于Shell函數的7種用法介紹的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 日日夜夜天天干干| 无套内谢A片毛片免费看| 一个人hd韩国| 女人大片AA高潮免费网站| 99亚洲精品自拍AV成人软件 | 狼人香蕉香蕉在线28看2019| 久久久久久精品人妻aⅴ东京热 | 多人乱p欧美4p| 久久久久毛片| tianlula天噜啦成人精品| 湿身影院| 妇欲欢公爽公妇高h欲| 国产在线观看首页123| bl和尚巨大撞到好爽h| 绝伦の上司に一晚人妻| 裸巨大的乳| juliaann熟妇五十欧美在线观看| 国产精品熟女乱婬999| 日本精品卡二卡三卡四卡2021| 狠狠色噜噜狠狠狠狠91| 道具play高h| 中国国语毛片免费观看视频 | 男男gaygays网站欧美| 国产成人精品免费网站在线观看 | 好爽?好紧?宝贝叫大声点| 国产91精品内裤包裹| 99热这里只有精品国产在热久久| 无码人妻精品一区二区二秋霞影院 | 又爽又黄A片免费观看啪啪 | 日韩色影院| 午夜免费播放观看在线视频| 成品动漫网站入口网页版怎样打开| 男人和女人一起差差差的app| 7777成年大片免费播放器| 夜夜贪欢〈高H〉| 92午夜福利少妇| 放荡的情欲k8| 黑人大荫道BBWBBB高潮潮喷| 国外黄色网址| 99精品国产成人一区二区在线| 色噜噜成人Av在线Av8|