$ cat test1 #!/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" $ $ ./test1 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 $
$ cat test4 #!/bin/bash # testing the exit status of a function func1() { echo"trying to display a non-existent file" ls -l badfile } echo"testing the function: " func1 echo"The exit status is: $?" $ $ ./test4 testing the function: trying to display a non-existent file ls: badfile: No such file or directory The exit status is: 1 $
案例2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ cat test4b #!/bin/bash # testing the exit status of a function func1() { ls -l badfile echo"This was a test of a bad command" } echo"testing the function:" func1 echo"The exit status is: $?" $ $ ./test4b testing the function: ls: badfile: No such file or directory This was a test of a bad command The exit status is: 0
$ cat test5 #!/bin/bash # using the return command in a function function dbl { read -p "Enter a value: " value echo"doubling the value" return $[ $value * 2 ] } dbl echo"The new value is $?" $ $ ./test5 Enter a value: 200 doubling the value The new value is 1 $
$ cat test5b #!/bin/bash # using the echo to return a value function dbl { read -p "Enter a value: " value echo $[ $value * 2 ] } result=$(dbl) echo"The new value is $result" $ $ ./test5b Enter a value: 200 The new value is 400 $ $ ./test5b Enter a value: 1000 The new value is 2000 $
$ cat badtest1 #!/bin/bash # trying to access script parameters inside a function function badfunc1 { echo $[ $1 * $2 ] } if [ $# -eq 2 ] then value=$(badfunc1) echo"The result is $value" else echo"Usage: badtest1 a b" fi $ $ ./badtest1 Usage: badtest1 a b $ ./badtest1 10 15 ./badtest1: * : syntax error: operand expected (error token is "* ") The result is
$ cat test7 #!/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 $ $ ./test7 Usage: badtest1 a b $ ./test7 10 15 The result is 150
$ cat test9 #!/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 $ $ ./test9 The result is 22 temp is smaller $
5. 数组
bash中的数组和C语言很像,特别是像为数组重命名的过程,和C语言中的很相似
如果试图将该数组变量作为函数参数,函数只会取数组变量的第一个值。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
$ cat badtest3 #!/bin/bash # trying to pass an array variable function testit { echo"The parameters are: $@" thisarray=$1 echo"The received array is ${thisarray[*]}" }
myarray=(1 2 3 4 5) echo"The original array is: ${myarray[*]}" testit $myarray $ $ ./badtest3 The original array is: 1 2 3 4 5 The parameters are: 1 The received array is 1
$ cat test10 #!/bin/bash # array variable to function test 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[*]} $ $ ./test10 The original array is 1 2 3 4 5 The new array value is: 1 2 3 4 5 $
$ cat test12 #!/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[*]}" $ $ ./test12 The original array is: 1 2 3 4 5 The new array is: 2 4 6 8 10
$ cat test13 #!/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" $ $ ./test13 Enter value: 5 The factorial of 5 is: 120
$ cat test14 #!/bin/bash # using functions defined in a library file
. ./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" $ $ ./test14 The result of adding them is: 15 The result of multiplying them is: 50 The result of dividing them is: 2
7.2 在.bashrc 文件中定义函数
在这之前,应该知道可以在命令行中使用函数,命令间使用分号隔开,这里不再详述了,详见书上372页
在命令行上直接定义shell函数的明显缺点是退出shell时,函数就消失了。
7.2.1 直接定义
在 .bashrc 文件中直接编辑函数,重新打开bash shell就会生效
1 2 3 4 5 6 7 8 9 10
$ cat .bashrc # .bashrc # Source global definitions if [ -r /etc/bashrc ]; then . /etc/bashrc fi function addem { echo $[ $1 + $2 ] } $
7.2.2 读取函数文件
source命令 + 文件路径 → .bashrc文件末尾
1 2 3 4 5 6 7 8 9
$ cat .bashrc # .bashrc # Source global definitions if [ -r /etc/bashrc ]; then . /etc/bashrc fi . /home/rich/libraries/myfuncs $