Shell函式的7種用法介紹

2021-07-09 02:40:05 字數 3635 閱讀 7848

1. 在shell檔案內部定義函式並引用:

複製**

**如下:

[~/shell/function]# cat factorial.sh 

#!/bin/bash

function 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

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

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()

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()

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()

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 $

function array()

array $

[~/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() * 2 ]     }

echo  new value:$ }

result=`array $`

echo $

[~/shell/function]# ./array1.sh 

parameters : 11 12 13 14 15 new value:22 24 26 28 30

from:  

Shell函式的7種用法介紹

1.在shell檔案內部定義函式並引用 複製 如下 shell function cat factorial.sh bin bash function factorial echo 程式名 0,用於求階乘 factorial 1 shell function factorial.sh 10 程式名 ...

的7種用法

jquery物件是乙個類陣列的物件,含有連續的整形屬性以及一系列的jquery方法。它把所有的操作都包裝在乙個jquery 函式中,形成了統一 也是惟一 的操作入口。其中我們用的非常頻繁的乙個函式是 或者說是jquery 當我們呼叫他的時候會根據傳入的引數的不同而達到不同的效果。簡要的說是 接收乙個...

shell中IF的用法介紹

一 語法結構 if condition then statements elif condition then statements.else statements fi二 說明 1 condition 注意condition前後要有空格 非空返回true,可使用 驗證 0為true,1為false...