简单的Shell函数实例

一、Shell函数

本教程目前为止所有脚本都是从头到尾执行。这样做很好,但你也许已经注意到有些脚本段间互相重复。

shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数。

shell中函数的定义格式如下:

复制代码代码如下:

函数名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

如果愿意,可在函数名前加上关键字function,这取决于使用者。


代码如下:

function 函数名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

函数返回值,可以显示增加return语句;如果不加,则将最后一条命令运行结果作为返回值(一般为0,如果执行失败则返回错误代码)。 return后跟数值(0-255)。

函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中。函数不必包含很多语句或命令,甚至可以只包含一个echo语句,这取决于使用者。

下面的例子定义了一个函数并进行调用: 

复制代码代码如下:

#!/bin/bash
demoFun(){   
     echo "This is your first shell function!"
 }
echo "Function begin..."
demoFun
echo "Function end!"

输出:
Function begin...
This is your first shell function!
Function end!

下面定义一个带有return语句的函数: 

复制代码代码如下:

#!/bin/bash
funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of two numbers is $? !"

输出类似下面:
The function is to get the sum of two numbers...
Input first number: 25
Input another number: 50
The two numbers are 25 and 50 !
The sum of two numbers is 75 !

函数返回值在调用该函数后通过 $? 来获得。

注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

二、Shell函数参数

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

带参数的函数示例:

复制代码代码如下:

#!/bin/bash
funWithParam(){
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The value of the tenth parameter is $10 !"
    echo "The value of the tenth parameter is ${10} !"
    echo "The value of the eleventh parameter is ${11} !"
    echo "The amount of the parameters is $# !"
    echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

输出:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数

使用return命令

复制代码

#!/bin/bash
# testing the script
function myfun {
  read -p "Enter a value:" value
  echo "double the value"
  return $[ $value * 2 ]
}

myfun

echo "The new vlue is $?"

复制代码

结果:
Enter a value:23
double the value
The new vlue is 46
(退出状态的取值范围是0到255,$?是最近已执行命令的退出状态)

使用函数输出,这种方法最靠谱了
看例子

复制代码

#!/bin/bash
# testing the script
function myfun {
  read -p "Enter a value:" value
  echo $[ $value * 2 ]
}

result=`myfun`

echo "The new vlue is $result"

复制代码

(这样就能很好的利用输出结果了)

在函数中使用变量
向函数传递参数

复制代码

#!/bin/bash
# testing the script
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 "Just one number 10:"value=`addem 10`
echo $value
echo -n "No numbers:"value=`addem`
echo $value
echo -n "More than two numbers:"vaule=`addem 10 15 20`
echo $value

复制代码

结果:
Adding 10 and 15:25
Just one number 10:20
No numbers:-1
(由上述可知,#可以得到参数的个数,1表示第一个参数,$2表示第二个参数)

复制代码

#!/bin/bash
# testing the script
function addem {  if [ $# -eq 0 ]||[ $# -gt 2 ]
  then
    echo -1
  elif [ $# -eq 1 ]
  then
    echo $[ $1 + $1 ]  else
    echo $[ $1 + $2 ]
  fi
}if [ $# -eq 2 ]
then
  value=`addem $1 $2`
  echo "The value is $value"else
  echo "Usage:test1 a b"fi

复制代码

(函数外面的#表示调用脚本使用的参数数,函数里的#表示调用函数使用的参数数,注意这点区别)

作用域是变量的可见区域。
全局变量,在shell脚本内处处有效的变量。函数中定义全局变量,那么主代码中有效。主代码中定义全局,函数中也有效。
默认情况下,脚本中定义的变量都是全局变量。
看例子:

复制代码

#!/bin/bash
# testing the script
function myfun {
  value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"

复制代码

(输入45,得到90,这里的value在函数中发生变化了,到脚本中一样可以使用,而且已经变化了)

局部变量
作用范围只在函数当中
关键字local

复制代码

#!/bin/bash
# testing the script
function myfun {
  local value=$[ $value * 2 ]
}
read -p "Enter a value:" value
myfun
echo "The new value is:$value"

复制代码

(输入45,输出45。因为加上local关键字之后,函数中的value是局部变量,与外界无关)

复制代码

#!/bin/bash
# testing the script
function myfun {
  local value=$[ $value * 2 ]
  echo $value
}
read -p "Enter a value:" value
result=`myfun`
echo "The new value is:$value"echo "The result of the fun is $result"

复制代码

(不过可以通过输出来获取函数处理的结果,这里的result还是处理后的结果,比如输入45,处理后是90)

数组变量与函数

复制代码

#!/bin/bash
# testing the script
function 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 original array is :${myarray[*]}"arg=`echo ${myarray[*]}`
result=`addarray $arg`
echo "The result is $result"

复制代码

结果:
The original array is :1 2 3 4 5
The result is 15
(数组参数传入函数,函数获取后,进行处理输出。脚本将输出结果打印)

复制代码

#!/bin/bash
# testing the script
function addarray {
  local sum=0
  local newarray
  newarray=(`echo "$@"`)  for value in ${newarray[*]}  do
    sum=$[ $sum + $value ]
  done
  echo ${newarray[*]}
}
myarray=(1 2 3 4 5)
echo "The original array is :${myarray[*]}"arg=`echo ${myarray[*]}`
result=`addarray $arg`
echo "The result is $result"

复制代码

(输出数组)

函数递归

复制代码

#!/bin/bash
# testing the script
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"

复制代码

(输入5,输出120。大环套小环,小环套更小,逐步执行。
我们一步一步剖析
5输入得到5*4!
4又得到4*3!
3又得到3*2!
2又得到2*1
由此得到5*4*3*2*1也就是120


显然单个脚本有助于减少输入量,但是如果多个脚本碰巧使用同样的函数又该怎样?
创建库

复制代码

#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

#!/bin/bash
# testing the script
source ./myfuncs

result=`addem 10 15`
echo "The result is $result"

(通过source就能使用库函数了)

#!/bin/bash
# testing the script
. ./myfuncs

result=`addem 10 15`
echo "The result is $result"
(或者点操作符,效果是一样的)

在命令行中使用函数
[root@localhost shellscript]# function multem {
> echo [1 * $2 ]
> }
[root@localhost shellscript]# multem 2 5
10


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值