文章目錄
- 一、Shell 函式
- 二、Shell 函式定義
- 三、函式回傳值
- 四、函式傳參
- 五、函式變數的作用范圍
- 六、遞回
- 1、階乘
- 2、遞回目錄
- 七、函式庫
一、Shell 函式
1、將命令序列按格式寫在一起
2、可方便重復使用命令序列
二、Shell 函式定義
方式一:
function 函式名 {
命令序列
}
方式二:
函式名 () {
命令序列
}
三、函式回傳值
return表示退出函式并回傳一個退出值,腳本中可以用 $? 變數顯示該值
使用原則:
1、函式一結束就取回回傳值,因為 $? 變數只回傳執行的最后一條命令的退出狀態碼
2、退出狀態碼必須是0-255,超出時值將為除以256取余
舉例:
方式一:
[root@gcc jiaoben1]#vim test3.sh
#!/bin/bash
function abc { #使用function進行函式定義
read -p "請輸入:" a
a=$[$a*2]
return $a #return表示退出函式并回傳一個退出值,腳本中可以用 $? 變數顯示該值
}
abc
echo $? #退出狀態碼必須是0-255,超出時值將為除以256取余
---------------------------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test3.sh
[root@gcc jiaoben1]#./test3.sh
請輸入:3
6
方式二:
[root@gcc jiaoben1]#vim test4.sh
#!/bin/bash
abc () {
read -p "請輸入:" a
a=$[$a*2]
echo $a
}
abc #直接使用函式名進行運算
或者
result='abc'
echo $result
----------------------------------------------------------------
[root@gcc jiaoben1]#./test4.sh
請輸入:400
800
四、函式傳參
在Shell中,呼叫函式時可以向其傳遞引數,在函式體內部,通過 $n 的形式來獲取引數的值,例如,$1表示第一個引數,$2表示第二個引數…即使用位置引數來實作引數傳遞,
方式一:
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
sum1 () {
sum=$[$1 + $2]
echo $sum
}
read -p "請輸入第一個引數:" first
read -p "請輸入第二個引數:" second
sum1 $first $second
----------------------------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test5.sh
[root@gcc jiaoben1]#./test5.sh
請輸入第一個引數:2
請輸入第二個引數:3
5
方式二:
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
sum1 () {
sum=$[$1 + $2]
echo $sum
}
sum1 $1 $2
---------------------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh 10 20
30
五、函式變數的作用范圍
函式在shell腳本中僅在當前shell環境中有效
shell腳本中的變數默認全域有效
將變數限定在函式內部使用local命令
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
a=5
b=6
echo "c等于$c"
}
a=8
c=9
abc
echo "a等于$a"
echo "b等于$b"
---------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
c等于9
a等于5
b等于6
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
local i
i=8
echo "inside $i"
}
i=9
abc
echo "outside $i"
------------------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside 8
outside 9
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
echo "inside1 $i"
local i
i=8
echo "inside2 $i"
}
i=9
abc
echo "outside $i"
----------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside1 9
inside2: 8
outside 9
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
echo "inside1 $i"
let i++
local i
i=8
echo "inside2: $i"
}
i=9
abc
echo "outside $i"
----------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside1 9
inside2: 8
outside 10
六、遞回
函式呼叫自己本身的函式
1、階乘
[root@gcc jiaoben1]#vim test6.sh
#!/bin/bash
fact () {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[$1 - 1]
local result=$(fact $temp)
echo "$[$1*$result]"
fi
}
read -p "請輸入階乘數:" n
result=`fact $n`
echo "$result"
-----------------------------------------------
[root@gcc jiaoben1]#./test6.sh
請輸入階乘數:3
6
2、遞回目錄
[root@gcc jiaoben1]#vim test7.sh
#!/bin/bash
function list_files {
for f in `ls $1`
do
if [ -d "$1/$f" ]
then
echo "$2$f"
list_files "$1/$f" "$2"
else
echo "$2$f"
fi
done
}
list_files "/var/log" ""
七、函式庫
函式庫只包含函式的定義,腳本中既包含函式的定義也包括可執行的代碼,
[root@gcc jiaoben1]#vim test8.sh
#!/bin/bash
jiafa () {
result=$[$1 + $2]
echo $result
}
jianfa () {
result=$[$1 - $2]
echo $result
}
chengfa () {
result=$[$1 * $2]
echo $result
}
chufa () {
if [ $2 -ne 0 ]
then
result=$[$1 / $2]
echo $result
else
echo "$2不能等于0!"
fi
}
----------------------------------------------------------------------------
[root@gcc jiaoben1]#vim test9.sh
#!/bin/bash
. /opt/jiaoben1/test8.sh
read -p "輸入第一個引數值:" first
read -p "輸入第二個引數值:" second
result1=`jiafa $first $second`
result2=`jianfa $first $second`
result3=$(chengfa $first $second)
result4=$(chufa $first $second)
echo $result1
echo $result2
echo $result3
echo $result4
--------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test9.sh
[root@gcc jiaoben1]#./test9.sh
輸入第一個引數值:20
輸入第二個引數值:10
30
10
200
2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/239607.html
標籤:其他
