##1.建立函数 函数的本质就是一个准备好了的代码块,有两种方式能够建立函数:shell
function name { commands }
name() { commands }
####1.使用函数 例子:编程
#!/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 is the end of the script" #
####2.返回值(以及使用return命令)数组
#!/bin/bash # func1() { echo "Tring to display a non-exitent file" ls -l badfile } # echo "testing the function:" func1 echo "The exit status is: $?" #使用$?得到返回值 #这里将返回2,由于ls 失败 # #若是将函数内命令的次序交换 func1() { ls -l badfile echo "Tring to display a non-exitent file" } #脚本最后将返回0,由于echo执行成功
使用return命令容许指定一个整数来定义返回值,但要注意:bash
####3.使用函数进行输出 命令的输出能够保存到shell,函数的输出也能够保存到shell编程语言
#!/bin/bash #using the return command in a function # function dbl { read -p "Enter a value " value echo $[ $value * 2 ] #整数运算 } # result=$(dbl) #获取输出 echo "The new value is $result"
##2.在函数中使用变量和数组 ####1.使用变量ide
#!/bin/bash #passing parameter to a function #一个简单的例子 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 echo -n "Let's try adding just one number:" value=$(addem 10) echo $value echo echo -n "Now trying adding no numbers:" value=$(addem) echo $value echo echo -n "Finally,try adding three numbers:" value=$(addem) echo $value
#!/bin/bash #trying to access script parameters inside a function # function func7 { echo $[ $1 * $2 ] } # if [ $# -eq 2 ] then value=$(func7 $1 $2) #手动传过去 echo "The result is $value" else echo "Usage: badtest1 a b" fi
####2.在函数中处理变量 函数中有两种变量,全局变量和局部变量函数
#!/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 $result ] then echo "temp is larger" else echo "temp is smaller" fi
####3.在函数中处理数组变量 在函数中若是试图把数组变量当成函数参数的话,函数只会取第一个值。因此,咱们须要将数组打散,而后再从新组合起来。oop
#!/bin/bash #adding values in an array # function addarray { local sum=0 local newattay newarray=($(echo "$@")) for value in ${newarray[*]} do sum=$[ $sum + $value ] done echo $sum } # myarray=(1 2 3 4 5 6) echo "The original array is: ${myarray[*]}" arg1=$(echo ${myarray[*]}) #利用echo将数组一个个输出 result=$(addarray $arg1) echo "The result is $result"
从函数返回数组也是同样的命令行
#!/bin/bash #returning an array value # function arraydblr { local origarray local newarray local elements local i origarray=($(echo "$@")) newarray=($(echo "$@")) elements=$[ $# - 1 ] #这个for的用法颇有价值,科学运算里,仍是处理数组比较多 for ((i = 0; i <= $elements; i++)) { newarray[$i]=$[ ${origarray[$i]} * 2 ] } echo ${newarray[*]} } # myarray=(1 2 3 4 5 6) echo "The origiarray is: ${myarray[*]}" arg1=$(echo ${myarray[*]}) result=($(arraydblr $arg1)) echo "The new array is: ${result[*]}" #打代码要集中精力,我这里报错了好多,由于我有时拼对了,有时拼错变量名……
####4.函数的递归 这个就是说,函数能够调用本身来获得结果code
#!/bin/bash #阶乘函数,但处理的值不能太大 # 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" #这种代码,当心无限循环
##3.建立库(source命令) 经过建立函数库,能够在不一样脚本中引用函数,极大的减小了工做量。
source命令能够使库在当前shell下使用,它有个快捷方式那就是 .
#!/bin/bash #这个脚本 #假定myfuncs与脚本在同一个目录下,若是不是,要有相应的路径。 . ./myfuncs #注意点与点之间有一个空格 # value1=10 value2=5 # echo "The two numbers are 10 and 5" # result1=$(addem $value1 $value2) result2=$(multem $value1 $value2) result3=$(divem $value1 $value2) # echo echo "The result of adding them is: $result1" echo "The result of multiplaying them is: $result2" echo "The result of dividing them is: $result3"
##4.在命令行上使用函数
要是不当心覆盖了原函数就很糟糕了。太危险了,就很少讲了。
#将如下代码加入到.bashrc中 # if [ -r /etc/bashrc ]; then . /etc/bashrc #注意空格 fi # . /home/rich/libraries/myfuncs #本身的库的路径,注意空格 #这样,shell就能够直接使用定义好的函数了
##5.shtool函数库 下载地址:ftp://ftp.gnu.org/gnu/shtool/shtool-2.0.8.tar.gz 安装方式: 解压缩到home中,进入shtool文件夹,运行:
./configure #检查构建shtool库文件所必须的软件 make #构建库文件 make test #检测 sudo make install #安装
函数的部分列表,见书p376
使用的例子:
#!/bin/bash # shtool platform #返回平台号,个人是Ubuntu 16.04 (AMD)