17.1 基本的脚本函数
17.1.1 创建函数
格式1:
function name {
commands
}
格式2:
name() {
commands
}
17.1.2 使用函数
#!/bin/bash
# using a function in a script
function func1 {
echo "This is an example of a function"
}
count=1
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func1
echo "Now this is the end of the script"
17.2 返回值
就是退出码
17.2.1 默认退出状态码
函数中最后一条命令的退出状态码
$?
: 查看退出状态码
#!/bin/bash
# testing the exit status of a function
func1() {
echo "trying to display a non-existent file"
ls -l badfile
}
echo "testing the function: "
func1
echo "The exit status is: $?"
17.2.2 使用 return 命令
注意:
- 函数一结束就取返回值
- 退出状态码必须是0~255
#!/bin/bash
# using the return command in a function
function dbl {
read -p "Enter a value: " value
echo "doubling the value"
return $[ $value * 2 ]
}
dbl
echo "The new value is $?"
17.2.3 使用函数输出
可以返回任意数据:
- 浮点值
- 字符值
#!/bin/bash
# using the echo to return a value
function dbl {
read -p "Enter a value: " value
echo $[ $value * 2 ]
}
result=$(dbl)
echo "The new value is $result"
17.3 在函数中使用变量
17.3.1 向函数传递参数
$0
: 函数名$1~$9
: 参数$#
: 参数个数- 必须将参数与函数名放在一行
func1 $value1 10
使用:
#!/bin/bash
# passing parameters to a function
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $1 + $2 ]
fi
}
echo -n "Adding 10 and 15: "
value=$(addem 10 15)
echo $value
echo -n "let's try adding just on number: "
value=$(addem 10)
echo $value
echo -n "Now trying adding no numbers: "
value=$(addem)
echo $value
echo -n "Finally, try adding three numbers: "
value=$(addem 10 15 20)
echo $value
将命令行中的参数传递给函数, 如果不这么做则出错:
#!/bin/bash
# trying to access script parameters inside a function
function func7 {
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ]
then
value=$(func7 $1 $2) # 重点
echo "The result is $value"
else
echo "Usage: badtest1 a b"
fi
$ ./test7
Usage: badtest1 a b
$ ./test7 10 15
The result is 150
17.3.2 在函数中处理变量
函数使用两种类型的变量:
- 全局变量
- 局部变量
1. 全局变量
默认情况, 所定义的变量都是全局变量
#!/bin/bash
# using a global variable to pass a value
function dbl {
value=$[ $value * 2 ]
}
read -p "Enter a value: " value
dbl
echo "The new value is: $value"
2. 局部变量
声明:
local temp
// 其它形式
local temp=$[ $value + 5 ]
使用:
#!/bin/bash
# demonstrating the local keyword
function func1 {
local temp=$[ $value + 5 ]
result=$[ $temp * 2 ]
}
temp=4
value=6
func1
echo "The result is $result"
if [ $temp -gt $value ]
then
echo "temp is larger"
else
echo "temp is smaller"
fi
$ ./test9
The result is 22
temp is smaller
17.4 数组变量和函数
17.4.1 向函数传数组参数
- 传递前打散
- 函数内合并
#!/bin/bash
# array variable to function test
function testit {
local newarray
newarray=(;'echo "$@"') # 合并
echo "The new array value is: ${newarray[*]}"
}
myarray=(1 2 3 4 5)
echo "The original array is ${myarray[*]}"
testit ${myarray[*]} # 打散
$
$ ./test10
The original array is 1 2 3 4 5
The new array value is: 1 2 3 4 5
17.4.2 从函数返回数组
- 函数输出打散的值
- 脚本中合并
#!/bin/bash
# returning an array value
function arraydblr {
local origarray
local newarray
local elements
local i
origarray=($(echo "$@"))
newarray=($(echo "$@"))
elements=$[ $# - 1 ]
for (( i = 0; i <= $elements; i++ ))
{
newarray[$i]=$[ ${origarray[$i]} * 2 ]
}
echo ${newarray[*]} # 打散
}
myarray=(1 2 3 4 5)
echo "The original array is: ${myarray[*]}"
arg1=$(echo ${myarray[*]})
result=($(arraydblr $arg1)) # 合并
echo "The new array is: ${result[*]}"
$
$ ./test12
The original array is: 1 2 3 4 5
The new array is: 2 4 6 8 10
17.5 函数递归
阶乘:
- x! = x * (x-1)!
function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 - 1 ]
local result='factorial $temp'
echo $[ $result * $1 ]
fi
}
17.6 创建库
bash shell 允许创建函数库文件, 在多个脚本中引用:
- 创建函数
# my script functions
function addem {
echo $[ $1 + $2 ]
}
function multem {
echo $[ $1 * $2 ]
}
function divem {
if [ $2 -ne 0 ]
then
echo $[ $1 / $2 ]
else
echo -1
fi
}
- 引用函数
source 命令会在当前 shell 上下文中执行命令, 而不是创建一个新 shell.
**
- source 的别名:
.
#!/bin/bash
# using functions defined in a library file
. ./myfuncs # 执行库脚本
value1=10
value2=5
result1=$(addem $value1 $value2)
result2=$(multem $value1 $value2)
result3=$(divem $value1 $value2)
echo "The result of adding them is: $result1"
echo "The result of multiplying them is: $result2"
echo "The result of dividing them is: $result3"
$
$ ./test14
The result of adding them is: 15
The result of multiplying them is: 50
The result of dividing them is: 2
17.7 在命令行上使用函数
17.7.1 在命令行上创建函数
单行方式:
- 每个命令后面需要加
;
$ function divem { echo $[ $1 / $2 ]; }
$ divem 100 5
20
多行方式:
$ function multem {
> echo $[ $1 * $2 ]
> }
$ multem 2 5
10
17.7.2 在 .bashrc 文件中定义函数
好处是重启系统后, 函数仍在.
1. 直接定义函数
直接在 .bashrc 中编写函数
2. 读取函数文件
在 .bashrc 中使用:
. /path/to/script
shell 还会将定义好的函数传给子 shell 进程
17.8 实例
GUN shtool shell