Linux Shell筆記之函式
阿新 • • 發佈:2019-01-06
1.建立函式
#!/bin/bash
function func1 { function關鍵字建立函式
echo "this is func1"
}
func2() { 接近其它語言形式的函式
echo "this is func2"
}
func1 引用函式
func2
2.函式返回值
1)退出狀態碼
[[email protected] function]# vi creatfun
#!/bin/bash
function func1 {
echo "this is func1"
}
func2() {
echo "this is func2"
ls badfie
}
func1
echo "func1's exit status is : $?" 使用$?變數存放退出狀態碼,由函式中的最後一條語句決定,因此使用此方法很危險
func2
echo "func2's exit status is : $?"
2)return命令
#!/bin/bash
func3() {
read -p "enter a number: " value
return $[ $value + 1 ]
}
func3
echo "func3's exit status is $?"
# ./func3
enter a number: 8
func3's exit status is 9 退出狀態碼需在0~255之間
3)使用函式輸出
#!/bin/bash
func3() {
read -p "enter a number: " value
echo $[ $value + 1 ] echo顯示值
}
result=`func3` 將函式輸出賦值給新變數
echo "new value is $result"
3.函式中使用變數
1)向函式中傳遞引數
#!/bin/bash
func4() {
echo $[ $1 + $2 ] shell將函式當做小型指令碼對待,向函式中傳參,與普通指令碼一樣
}
if [ $# -eq 2 ]
then
value=`func4 $1 $2` 函式不能直接從指令碼命令列中獲取引數值,需要先在指令碼中獲取,再手動向函式中傳值
echo "new value is $value"
else
echo "usage: func4 a b"
fi
# ./func4 3 4
new value is 7
2)函式中處理變數
[ [email protected] function]# vi func5
#!/bin/bash
func5() {
local temp=$[ $value +3 ] local關鍵字,定義區域性變數
result=$[ $temp * 2 ]
}
temp=4
value=6 預設情況下,指令碼中定義的任何變數均為全域性變數,可以函式內訪問
func5
echo "new temp value is $temp"
echo "result value is $result"
3)向函式中傳遞陣列變數
#!/bin/bash
addarray() {
local sum=0
local newarray
newarray=(`echo " [email protected]"`) 從命令列引數中重建陣列變數
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "the old array is : ${myarray[*]}"
result=`addarray ${myarray[*]}` 將陣列變數的值分解成單個值然後將這些值作為函式引數使用
echo "final result is $result"
4)遞迴
#!/bin/bash
factorical() {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 - 1 ]
local result=`factorical $temp`
echo $[ $result * $1 ]
fi
}
read -p "enter a number: " value
result=`factorical $value`
echo "the factorical of $value is $result"
4.建立函式庫
1)使用函式庫
# cat myfuncs
#!/bin/bash
addem() {
echo $[ $1 + $2 ]
}
multem() {
echo $[ $1 * $2 ]
}
divem() {
if [ $2 -ne 0 ]
then
echo $[ $1 / $2 ]
else
echo -1
fi
}
#!/bin/bash
. ./myfuncs 使用函式的關鍵字為source命令,快捷別名為.號,在當前shell上下文中執行命令
num1=4
num2=2
add=`addem $num1 $num2`
mul=`multem $num1 $num2`
div=`divem $num1 $num2`
echo "add result is $add"
echo "mult result is $mul"
echo "div result is $div"
2)在.bashrc檔案中定義函式
$ vi .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
alias ll='ls -al'
. /home/hunterno4/function/myfuncs
$ addem 3 4 如此再次啟動shell時,就可以像使用命令一樣使用函數了
7
#!/bin/bash
function func1 { function關鍵字建立函式
echo "this is func1"
}
func2() { 接近其它語言形式的函式
echo "this is func2"
}
func1 引用函式
func2
2.函式返回值
1)退出狀態碼
[[email protected] function]# vi creatfun
#!/bin/bash
function func1 {
echo "this is func1"
}
func2() {
echo "this is func2"
ls badfie
}
func1
echo "func1's exit status is : $?" 使用$?變數存放退出狀態碼,由函式中的最後一條語句決定,因此使用此方法很危險
func2
echo "func2's exit status is : $?"
2)return命令
#!/bin/bash
func3() {
read -p "enter a number: " value
return $[ $value + 1 ]
}
func3
echo "func3's exit status is $?"
# ./func3
enter a number: 8
func3's exit status is 9 退出狀態碼需在0~255之間
3)使用函式輸出
#!/bin/bash
func3() {
read -p "enter a number: " value
echo $[ $value + 1 ] echo顯示值
}
result=`func3` 將函式輸出賦值給新變數
echo "new value is $result"
3.函式中使用變數
1)向函式中傳遞引數
#!/bin/bash
func4() {
echo $[ $1 + $2 ] shell將函式當做小型指令碼對待,向函式中傳參,與普通指令碼一樣
}
if [ $# -eq 2 ]
then
value=`func4 $1 $2` 函式不能直接從指令碼命令列中獲取引數值,需要先在指令碼中獲取,再手動向函式中傳值
echo "new value is $value"
else
echo "usage: func4 a b"
fi
# ./func4 3 4
new value is 7
2)函式中處理變數
[
#!/bin/bash
func5() {
local temp=$[ $value +3 ] local關鍵字,定義區域性變數
result=$[ $temp * 2 ]
}
temp=4
value=6 預設情況下,指令碼中定義的任何變數均為全域性變數,可以函式內訪問
func5
echo "new temp value is $temp"
echo "result value is $result"
3)向函式中傳遞陣列變數
#!/bin/bash
addarray() {
local sum=0
local newarray
newarray=(`echo "
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo $sum
}
myarray=(1 2 3 4 5)
echo "the old array is : ${myarray[*]}"
result=`addarray ${myarray[*]}` 將陣列變數的值分解成單個值然後將這些值作為函式引數使用
echo "final result is $result"
4)遞迴
#!/bin/bash
factorical() {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 - 1 ]
local result=`factorical $temp`
echo $[ $result * $1 ]
fi
}
read -p "enter a number: " value
result=`factorical $value`
echo "the factorical of $value is $result"
4.建立函式庫
1)使用函式庫
# cat myfuncs
#!/bin/bash
addem() {
echo $[ $1 + $2 ]
}
multem() {
echo $[ $1 * $2 ]
}
divem() {
if [ $2 -ne 0 ]
then
echo $[ $1 / $2 ]
else
echo -1
fi
}
#!/bin/bash
. ./myfuncs 使用函式的關鍵字為source命令,快捷別名為.號,在當前shell上下文中執行命令
num1=4
num2=2
add=`addem $num1 $num2`
mul=`multem $num1 $num2`
div=`divem $num1 $num2`
echo "add result is $add"
echo "mult result is $mul"
echo "div result is $div"
2)在.bashrc檔案中定義函式
$ vi .bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
alias ll='ls -al'
. /home/hunterno4/function/myfuncs
$ addem 3 4 如此再次啟動shell時,就可以像使用命令一樣使用函數了
7