Shell脚本编程---创建函数(七)

一、基本脚本函数

(1)创建函数

在bash shell脚本中创建函数可以使用两种格式。

一种格式是使用关键字function:

function name {
    commands
}

-name属性定义了该函数的唯一名称。
-comands是组成函数的一条或多条bash shell命令

另一种定义函数的方式:

name() {
commands
}

(2)使用函数

[root@ceph01 function]# cat function.sh 
#!/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 "Now this is the end of the script"

运行脚本:

[root@ceph01 function]# ./function.sh 
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
Now this is the end of the script

二、返回值

(1)默认退出状态($?)

[root@ceph01 function]# cat function1.sh 
#!/bin/bash
# testing the exit status of a function
fun1() {
	echo "trying to display a non-existent file"
	ls -l function.sh
}

echo "testing the function:"
fun1
echo "The exit status is: $?"

运行脚本:

[root@ceph01 function]# ./function1.sh 
testing the function:
trying to display a non-existent file
-rwxrwxrwx 1 root root 216 Jan  6 19:26 function.sh
The exit status is: 0

(2)使用return命令

[root@ceph01 function]# cat return.sh 
#!/bin/bash
# using the return command in a function

function db1 {
	read -p "Ether a value: " value
	echo "doubling the value"
	return $[ $value * 2 ]
}

db1
echo "The new value is $?"

运行脚本:

[root@ceph01 function]# ./return.sh 
Ether a value: 4
doubling the value
The new value is 8

(3)使用函数输出

[root@ceph01 function]# cat function2.sh 
#!/bin/bash
# using the echo to return a value

function db1 {
	read -p "Enter a value: " value
	echo $[ $value * 2]
}
result=`db1`
echo "The new value is $result"

运行脚本:

[root@ceph01 function]# ./function2.sh 
Enter a value: 5
The new value is 10

三、在函数中使用变量

(1)向函数传递参数

[root@ceph01 function]# cat function3.sh 
#!/bin/bash
# passing parameters to a funcion

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 one number: "
value='addem 10'
echo $value

echo -n "Now trying adding no numbers: "
value='addem 10 15 20'
echo $value

运行脚本:

[root@ceph01 function]# ./function3.sh 
Adding 10 and 15: addem 10 15
Let's try adding just one number: addem 10
Now trying adding no numbers: addem 10 15 20

(2)在函数中处理变量

全局变量

[root@ceph01 function]# cat function5.sh 
#!/bin/bash
# using a global variable to pass a value 

function db1 {
	value=$[ $value * 2 ]
}

read -p "Enter a value: " value
db1
echo "The new value is: $value"


[root@ceph01 function]# ./function5.sh 
Enter a value: 10
The new value is: 20

局部变量

[root@ceph01 function]# cat function6.sh 
#!/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

[root@ceph01 function]# ./function6.sh 
The result is 22
temp is smaller

四、数组变量与函数

(1)向函数传递数组

#!/bin/bash
# adding values in an array

function addarray {
        local sum=0
        local newarray
        newarray=('echo "$@"')
        for value in ${newarry[*]}
        do
                sum=$[ $sum + $value ]
        done
        echo $sum
}

myarray=(1 2 3 4 5)
echo "The original array is: ${myarray[*]}"
arg1=`echo ${myaaray[*]}`
result=`addarray $arg1`
echo "The result is $result"

[root@ceph01 function]# ./function7.sh 
The original array is: 1 2 3 4 5
The result is 15

(2)从函数返回数组

[root@ceph01 function]# cat function8.sh 
#!/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[*]}"


[root@ceph01 function]# ./function8.sh 
The original array is: 1 2 3 4 5
The new array is: 2 4 6 8 10

五、函数递归

[root@ceph01 function]# cat function9.sh 
#!/bin/bash
# using recursion

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

read -p "Enter value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"

[root@ceph01 function]# ./function9.sh 
Enter value: 5
The factorial of 5 is: 120

六、创建库

[root@ceph01 function]# cat myfuncs 
#my script function

function addem {
	echo $[ $1 + $2 ]
}

function multem {
	echo $[ $1 * $2 ]
}

function divem {
	if [ $2 -ne 0 ]
	then
		echo $[ $1 / $2 ]
	else
		echo -1
	fi
}
[root@ceph01 function]# cat function10.sh 
#!/bin/bash
# using a library file the wrong way
. ./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"

[root@ceph01 function]# ./function10.sh 
The result of adding them is: 15
The result of multiplying them is: 50
The result of dividing them is: 2

七、在命令行中使用函数

(1)在命令行创建函数

第一种是将函数定义在一行命令中:

[root@ceph01 function]# function divem { echo $[ $1 / $2 ]; }
[root@ceph01 function]# divem 100 5
20

第二种方法是使用多行命令定义函数:

[root@ceph01 function]# function multem {
> echo $[ $1 * $2 ]
> }
[root@ceph01 function]# multem 2 6
12

(2)在.bashrc文件中定义函数

直接定义函数

[root@ceph01 function]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

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

function addem {
echo $[ $1 + $2 ]
}

提供函数文件

[root@ceph01 function]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

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

. /home/function/myfuncs

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值