这一节我们一起学习一下子Linux Shell 中的函数
一:函数的基本格式:
1: function name {
commands
}
2: name() {
}
二:函数的状态码:
默认情况下状态码返回函数中最后一条命令的状态码信息,并且状态码的数值范围是0~255之间,如果想要返回比他大的数值或者字符的时候,就需要使用return 语句
举例来说明一下不使用return语句的情况
#!/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"
echo "=====now start test func2===="
#func2
echo "is func2 exists"
func2(){
echo "hello I'm a function named func2"
echo "name is : $name"
}
func2
echo "the exit status is : $?"
echo "func2 is end"
输出如下:
this is an example of a function
this is an example of a function
this is an example of a function
this is an example of a function
this is an example of a function
this is the end of the loop
this is an example of a function
now this is the end of the script
=====now start test func2====
is func2 exists
hello I'm a function named func2
name is :
the exit status is : 0
func2 is end
其中$?表示返回的状态信息,另外函数的使用不能放到声明函数的前面
下面是使用了return 语句的例子
#!/bin/bash
function db {
read -p "Enter a value:" value
echo "doubling the value"
return $[ $value * 2 ]
}
db
echo "the new value is $?"
这里会将输入的值扩大两倍返回三:全局变量和局部变量
全局变量定义的时候前面不需要添加任何关键字,不论是在函数内部还是在函数外部,这一点跟java 或者C语言有些不同,定义局部变量的方法是在变量的前面添加关键字:local关键字
如下面的例子:全局变量
#!/bin/bash
function db {
value=$[ $value * 2 ]
}
read -p "Enter a value:" value
db
echo "the new value is : $value"
这里的变量就会使用全局变量的值,并且会将其扩大两倍
eg: 局部变量:local关键字
#!/bin/bash
function fun {
echo "只要变量前面不加local都认为是全局变量所以result是全局变量,在函数外
部可以引用它"
local temp=$[ $value + 5 ]
echo "temp is $temp"
result=$[ $temp * 2 ]
}
temp=4
value=6
fun
echo "the result is $result"
if [ $temp -gt $value ]
then
echo "temp is larger"
else
echo "temp is smaller"
fi
如果不在temp变量中加入local的话那么它会直接改变全局变量中temp的值,然而这里加了local关键字的时候不会该改变全局变量的值可以放心的使用全局变量了四:向函数中传递参数变量
当我们向函数中传递变量的时候,需要在函数名的后面添加参数值
eg:
#!/bin/bash
function addem {
if [ $# -eq 0 ] || [ $# -gt 2 ]
then
echo -1
elif [ $# -eq 1 ]
then
echo $[ $1 + $1 ]
else
echo $[ $2 + $1 ]
fi
}
echo -n "adding 10 and 15:"
value=$(addem $1 $2)
echo $value
echo
echo -n "Let's try adding just one number"
value=$(addem $1)
echo $value
echo
echo -n "Now trying adding no number:"
value=$(addem)
echo $value
echo
echo -n "finally ,try adding three numbers"
value=$(addem $1 $2 $3)
echo $value
这里$#的意思是参数的个数如果 =0或者 >2的时候返回-1 接收函数的返回值用的是$(addem $1 $2) 函数内部通过 echo $[ $1 + $2 ]返回数值,它会存入shell变量中
五:向函数返回数组参数
向脚本函数传递数组变量的方法会有点不好理解,将数组变量的当作单个参数传递的话,它不会起作用。
eg:
#!/bin/bash
function testit {
echo "the parameter are:$@"
thisarray=$1
echo "the received array is ${thisarray[*]}"
}
myarray=(1 2 3 4 5)
echo "如果你试图将该数组变量作为函数参数,函数只会取数组中的第一个值"
echo "=====myarrayp[*] will be show first value in an array list===="
echo "the original array is : ${myarray[*]}"
testit $myarray
输出如下:
如果你试图将该数组变量作为函数参数,函数只会取数组中的第一个值
=====myarrayp[*] will be show first value in an array list====
the original array is : 1 2 3 4 5
the parameter are:1
the received array is 1
要解决这个问题你必须将该数组变量分解成单个的值然后将这些值作为函数参数使用,在函数内部,可以将所有的参数重新组合成一个新的变量,下面是具体的例子
#!/bin/bash
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[*]}
其实echo "$@"是已经成为了一个shell变量了,这里使用$a变量就会成为一个数组变量
又如:
#!/bin/bash
function add {
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 original array is:${myarray[*]}"
arg1=$(echo ${myarray[*]})
result=$(add $arg1)
echo "the result is $result
首先声明一个全局数组,将该数组赋给一个参数传递给函数add在函数内部处理传递过来的数组参数,然后遍历数组,最后将数据打印其实是被存储到shell变量中去了,最后在函数外部输出它的值
六:从函数返回数组
#!/bin/bash
function returnarr {
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=($(returnarr $arg1))
echo "the new array is :${result[*]}"
the original array is :1 2 3 4 5the new array is :2 4 6 8 10
该脚本用$arg1变量将数组值传给returnarr函数,该函数将数组重组到新的数组变量中生成一个数组变量的副本,然后对数据元素进行遍历,最后函数使用echo语句来输出每个数组的值,