shell script中建立函数

1.基本的脚本函数

1.1建立函数

建立函数有2中格式:shell

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

1.2使用函数

直接引用函数名,函数定义必须在引用以前。函数名必须惟一,不然函数会被新的建立的同名函数覆盖掉,而不会报错。数组

1.3函数返回值 

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

1)默认退出状态码ide

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

2)使用return命令spa

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

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

3)使用函数输出递归

能够将函数输出保存到shell变量中,result=`functionname`three

#!/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表明函数名、$一、$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以后,新加入的函数就有效了)
相关文章
相关标签/搜索