wsx@wsx:~/tmp$ cat test1 #!/bin/bash # using a functionin 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"
wsx@wsx:~/tmp$ ./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
wsx@wsx:~/tmp$ cat test2 #!/bin/bash # testing the exit status of a function
func1() { trueecho "trying to display a non-existent file" truels -l badfile }
echo "testing the function" func1 echo "The exit status is: $?" wsx@wsx:~/tmp$ ./test2 testing the function trying to display a non-existent file ls: 无法访问'badfile': 没有那个文件或目录 The exit status is: 2
wsx@wsx:~/tmp$ cat test3 #!/bin/bash # testing the exit status of a function
func1(){ truels -l badfile trueecho "This was a test of a bad command" }
echo "testing the function:" func1 echo "The exit status is: $?" wsx@wsx:~/tmp$ ./test3 testing the function: ls: 无法访问'badfile': 没有那个文件或目录 This was a test of a bad command The exit status is: 0
func1 echo "The result is $result" if [ $temp -gt $value ] then trueecho "temp is larger" else trueecho "temp is smaller" fi wsx@wsx:~/tmp$ ./badtest2 The result is 22 temp is larger
func1 echo "The result is $result" if [ $temp -gt $value ] then trueecho "temp is larger" else trueecho "temp is smaller" fi
wsx@wsx:~/tmp$ ./test8 The result is 22 temp is smaller
数组变量和函数
在函数中使用数组变量值有点麻烦,还需要一些特殊考虑。下面我们使用一种方法来解决问题。
向函数传数组参数
这个方法有点不好理解,将数组变量当做单个参数传递的话不起作用,下面我们看一个bad例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
wsx@wsx:~/tmp$ cat badtest3 #!/bin/bash # trying to pass an array variable
function testit { trueecho "The parameters are: $@" truethisarray=$1 trueecho "The received array is ${thisarray[*]}" }
myarray=(1 2 3 4 5) echo "The original array is: ${myarray[*]}" testit $myarray wsx@wsx:~/tmp$ ./badtest3 The original array is: 1 2 3 4 5 The parameters are: 1 The received array is 1
wsx@wsx:~/tmp$ cat test10 #!/bin/bash # returning an array value
function arraydblr { truelocal origarray truelocal newarray truelocal elements truelocal i trueorigarray=($(echo "$@")) truenewarray=($(echo "$@")) trueelements=$[ $# - 1 ] truefor (( i = 0; i <= $elements; i++ )) true{ newarray[$i]=$[ ${origarray[$i]} * 2] true} trueecho ${newarray[*]} }
myarray=(1 2 3 4 5) echo "The orignal array is ${myarray[*]}" arg1=$(echo ${myarray[*]}) result=($(arraydblr $arg1)) echo "The new array is: ${result[*]}"
wsx@wsx:~/tmp$ ./test10 The orignal array is 1 2 3 4 5 The new array is: 2 4 6 8 10
wsx@wsx:~/tmp$ cat test12 #!/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"
运行:
1 2 3 4
wsx@wsx:~/tmp$ ./test12 The result of adding them is: 15 The result of multiplying them is: 50 The result of dividing them is: 2
wsx@wsx:~$ cd shtool-2.0.8/ wsx@wsx:~/shtool-2.0.8$ ./configure Configuring GNU shtool (Portable Shell Tool), version 2.0.8 (18-Jul-2008) Copyright (c) 1994-2008 Ralf S. Engelschall <rse@engelschall.com> checking whether make sets $(MAKE)... yes checking for perl interpreter... /usr/bin/perl checking for pod2man conversion tool... /usr/bin/pod2man configure: creating ./config.status config.status: creating Makefile config.status: creating shtoolize config.status: executing adjustment commands wsx@wsx:~/shtool-2.0.8$ make building program shtool ./shtoolize -o shtool all Use of assignment to $[ is deprecated at ./shtoolize line 60. Generating shtool...(echo 11808/12742 bytes)...(mdate 3695/4690 bytes)...(table 1818/2753 bytes)...(prop 1109/2038 bytes)...(move 2685/3614 bytes)...(install 4567/5495 bytes)...(mkdir 2904/3821 bytes)...(mkln 4429/5361 bytes)...(mkshadow 3260/4193 bytes)...(fixperm 1471/2403 bytes)...(rotate 13425/14331 bytes)...(tarball 5297/6214 bytes)...(subst 5255/6180 bytes)...(platform 21739/22662 bytes)...(arx 2401/3312 bytes)...(slo 4139/5066 bytes)...(scpp 6295/7206 bytes)...(version 10234/11160 bytes)...(path 4041/4952 bytes) building manpage shtoolize.1 building manpage shtool.1 building manpage shtool-echo.1 building manpage shtool-mdate.1 shtool-mdate.tmp around line 222: You forgot a '=back' before '=head1' POD document had syntax errors at /usr/bin/pod2man line 71. building manpage shtool-table.1 building manpage shtool-prop.1 building manpage shtool-move.1 building manpage shtool-install.1 building manpage shtool-mkdir.1 shtool-mkdir.tmp around line 186: You forgot a '=back' before '=head1' POD document had syntax errors at /usr/bin/pod2man line 71. building manpage shtool-mkln.1 building manpage shtool-mkshadow.1 shtool-mkshadow.tmp around line 191: You forgot a '=back' before '=head1' POD document had syntax errors at /usr/bin/pod2man line 71. building manpage shtool-fixperm.1 building manpage shtool-rotate.1 building manpage shtool-tarball.1 building manpage shtool-subst.1 building manpage shtool-platform.1 building manpage shtool-arx.1 building manpage shtool-slo.1 building manpage shtool-scpp.1 building manpage shtool-version.1 building manpage shtool-path.1