您的位置:首页 > 运维架构 > Shell

shell script中创建函数

2016-05-20 22:39 501 查看

1.基本的脚本函数

1.1创建函数

创建函数有2中格式:

function name {
command
}

name(){
command
}
#name后的圆括号为空


1.2使用函数

直接引用函数名,函数定义必须在引用之前。函数名必须唯一,否则函数会被新的创建的同名函数覆盖掉,而不会报错。

1.3函数返回值

bash shell会把函数当做小型脚本,运行结束时会返回一个退出状态码。有3中不同的方法生成退出状态码。

1)默认退出状态码

默认退出状态码是函数中最后一条命令返回的退出状态码。可以用$?来查看。

2)使用return命令

bash shell使用return命令来退出函数并返回特定的退出状态码。

但注意:函数一结束就取返回值;退出状态码必须在0~255之间。

3)使用函数输出

可以将函数输出保存到shell变量中,result=`functionname`

#!/bin/bash
#using the echo to return a value

function db1 {
read -p "Enter a value: " value     #注意技巧
echo $[ $value * 2 ]
}

rusult=`db1`
echo "The new value is $result"

函数用echo语句显示计算结果。技巧:db1函数输出了2条信息,read命令输出了一条询问用户输入值的消息。bash shell没有将它作为STDOUT输出的一部分,并且忽略掉它。·

1.4函数中变量的用法

1)向函数传递参数

bash shell 会将函数当成小型脚本,意味着我们可以向函数传递参数,同脚本类似,函数也可以使用标准的参数环境变量来代表命令行上传给函数的参数。如$0代表函数名、$1、$2.等定义参数,$#代表参数数目。

在脚本中使用带参函数时,函数和参数必须放在同一行,如:functionname 参数1 参数2...

注意:由于函数使用特殊参数环境变量作为自己的参数值,它不能直接从脚本的命令行获取脚本的参数值。必须在调用函数时,手动将它们传过去。

#!/bin/bash
#trying to access script parameters inside a function
function func1 {
echo $ [ $1 * $2 ]
}
if [ $# -eq 2 ]
then
value=`func1 $1 $2`        #此处注意。将脚本的参数传递给函数。。若改为value=`func1`无法执行成功
echo "The result is $value"
else
echo "usage : sh1.sh a b"
fi

2)在函数中处理变量——全局和局部

全局变量

全局变量在shell脚本中任何地方都有效,默认情况下,在脚本中定义的任何变量都是全局变量。函数可以使用全局变量,若函数改变了变量的值,这个值是有效的,函数外这个变量就变成了新值。

#!/bin/bash
#test variables

function func1 {
temp=$[ $value + 5 ]
result=$[ $temp * 2 ]
}
temp=4
value=6
func1
echo "the result is: $result"
echo "the temp is: $temp"
echo "the $value is: $value"

执行结果:
the result is: 22
the temp is: 11
the $value is: 6
#可以看到temp被函数func1赋予新值

局部变量

函数内部使用的变量可以声明为局部变量:local 变量

也可以在给变量赋值时使用local关键字。local关键字保证变量只局限在函数中。

#!/bin/bash
#test variables

function func1 {
local temp=$[ $value + 5 ]
result=$[ $temp * 2 ]
echo “the local temp is: $temp"
}
temp=4
value=6
func1
echo "the result is: $result"
echo "the temp is: $temp"
echo "the $value is: $value"

执行结果:
the local temp is: 11   #注意temp值,在函数内变化
the result is: 22
the temp is: 4     #注意temp值,在函数外没有
the $value is: 6


2.数组变量和函数

2.1数组定义

与C语言中数组类似,数组是能够存储多个值的变量,值可按单个值或整个数组来引用。值与值之间用空格分开。

数组定义:myarray=(one two three four five) 或者数字 myarrayn=(1 2 3 4 5 6 7)

echo $myarry 只显示第一个数one echo ${myarry[2]} 显示一个数 three echo ${myarry[*]} 显示整个数组one two three four five

可以用unset命令删除数组中的某个值,注意:值删掉了,但位置还在

$unset myarray[2]
$echo ${myarray[*]}
one two four five
$echo ${myarray[2]}

$echo ${myarray[3]}
four


2.2向函数传递数组

向函数传递数组时,必须使用functionname ${myarray[*]} 的方式传递,不能只写数组名$myarray,否则只能传递第一个数。

在接收数组值之后,可以在函数内重建数组,重建的数组任然可以像其他数组一样使用。

#!/bin/bash
#array variable test
function testit {
local newarray
newarray=($@)     #这几种重建方式都可以newarray=(`echo "$@"`)
local sum=0
for value in ${newarray[*]}
do
sum=$[ $sum + $value ]
done
echo $sum
}
wholearray=(1 2 3 4 5)
echo "the original array is :${wholearray[*]}"
result=`testit ${wholearray[*]}`     #注意传递数组的所有值
echo "the result is $result"

@lab403-1F:~/shell_script$ ./shell5.sh
the original array is :1 2 3 4 5
the result is 15


2.3从函数返回数组

就是在函数中构建数组,并重新返回给脚本。

#!/bin/bash
#array variable test
function testit {
local newarray=$@
#此处不加括号,运行是不会报错,因为newarray[$i]=$[ ${origarray[$i]} * 2 ]一句生成新的数组
local origarray=($@)
#此处若省略括号origarray=$@,会把所有值赋给origarray[0],echo ${origarray[0]},会输出1 2 3 4 5,   #而echo ${origarray[1]}没有值输出,运行时出错!!!
n=$[ $# - 1 ]
for (( i=0; i <= $n; i++ ))
{
newarray[$i]=$[ ${origarray[$i]} * 2 ]
}
echo ${newarray[*]}
}
wholearray=(1 2 3 4 5)
echo "the original array is :${wholearray[*]}"
result=`testit ${wholearray[*]}`
echo "the result is ${result[*]}"

执行结果:
yanfang@lab403-1F:~/shell_script$ ./shell5.sh
the original array is :1 2 3 4 5
the result is 2 4 6 8 10


3.递归函数

递归函数可以递归的调用自己,不需要使用任何外部资源。通常递归函数都有一个最终可以迭代到的基准值。计算阶乘的函数为例:

function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 - 1 ]
local result=`factorial temp`
echo $[ $result * $1 ]
fi
}


4.创建函数库

可以将多个脚本需要用函数写在同一个文件中,并在脚本中引用,脚本中引用库函数文件需要用source(.点操作符)命令。

注意:库文件的路径,若库文件与脚本在同一目录,可以用source ./functions 或者 . ./fuanctions;

若库文件与脚本不在同一目录,则用绝对路径: . /home/caishu/fuanctions

可以把函数写进 .bashrc文件(可以是用户的.bashrc,可以是系统的),shell每次启动都能重新载入的地方。

$ .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

# User specific aliases and functions
#直接定义的函数
function addem {
echo [[1 + $2 ]
}

#也可以在这里用source命令加函数库
. /home/caishu/functions
(重启shell之后,新加入的函数就有效了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: