括号的使用总结
单括号、双括号、单方括号、双方括号
- 单括号
()没作用; $()是命令替换;(())可以进行算数运算,c 语言风格的 for 命令;- 单方括号
[]是测试命令,需要有空格; $[]是数学运算,作用与$(())一样,后者更常用;$[[]]是高级的字符串运算,需要有空格;
数组
对其它 shell 而言,数组变量的可移植性并不好,应尽量减少使用。下面的例子在 zsh 中运行,结果就不一样。
#!/bin/bash# 定义数组myarray=(one two three four)# oneecho $myarray# oneecho ${myarray[0]}# one two three fourecho ${myarray[*]}myarray[2]=sevenunset myarray[2]# one two fourecho ${myarray[*]}# 这个位置是空的echo ${myarray[2]}# fourecho ${myarray[3]}unset myarray# 空的echo ${myarray[*]}
函数
创建函数
function name {commands}# 或者name() {commands}
注意,函数必需先定义,然后才能使用。
使用举例:
#!/bin/bash# 使用函数function func1 {echo "This is an example of a function"}count=1while [ $count -le 5 ]dofunc1count=$((count+1))done# 会覆盖上面的定义function func1 {echo "This is another example"}func1
返回值
有三种方式获取:
第一,默认最后一个命令的退出状态码;
#!/bin/bash# 不推荐使用func1() {echo "trying to display a non existent file"ls -l badfile}func1echo "The exit status is: $?"
第二,使用 return 命令;
#!/bin/bash# 使用 return 命令# 有两点需要注意:# 1. 函数一结束就取返回值# 2. 退出状态码必须是 0~255function db1 {read -p "Enter a value: " valueecho "doubling the value"return $((value * 2))}db1echo "The new value is $?"
第三,使用函数输出,推荐使用;
#!/bin/bashfunction db1 {read -p "Enter a value: " valueecho $((value * 2))}# 命令替换result=$(db1)echo "The new value is $result"
向函数传递参数
同脚本获取命令行参数一样,函数也使用 $1 … $9 … 获取参数。$0 是函数名。
#!/bin/bash# 向函数传递参数function addem {if [ $# -eq 0 ] || [ $# -gt 2 ]thenecho -1elif [ $# -eq 1 ]thenecho $(($1 + $1))elseecho $(($1 + $2))fi}echo "Adding 10 and 15: $(addem 10 15)"
在函数中使用变量
默认情况下,在脚本中定义的变量都是全局变量,在函数内外都可以使用。
#!/bin/bash# 默认都是全局变量function db1 {value=$((value * 2))}read -p "Enter a value: " valuedb1echo The new value is: $value
声明局部变量,在变量前加上 local 关键字。
#!/bin/bash# 定义局部变量function func1 {local temp=$((value+5))result=$((temp * 2))}temp=4value=6func1# 22echo "The result is $result"# 4echo "The temp value is $temp"
向函数传递数组
#!/bin/bash# 给函数传递数组wrong() {echo "The parameters are: $@"thisarray=$1echo "The received array is ${thisarray[*]}"}myarray=(1 2 3 4 5)echo "The origin array is ${myarray[*]}"# 这种传递数组的方式不行wrong $myarrayecho# 正确使用方法right() {echo "The parameters are: $@"# 在内部创建一个新的变量newarray=($(echo $@))echo "The received array is ${newarray[*]}"}right ${myarray[*]}# 结果是# The origin array is 1 2 3 4 5# The parameters are: 1# The received array is 1## The parameters are: 1 2 3 4 5# The received array is 1 2 3 4 5
函数返回数组
递归
#!/bin/bash# 递归# 计算一个数的阶乘function factorial {if [ $1 -eq 1 ]thenecho 1elselocal temp=$(($1 - 1))local resultresult=$(factorial $temp)echo $((result * $1))fi}read -p "Enter value: " valueresult=$(factorial $value)echo "The factorial of $value is: $result"
创建库
创建库文件,然后在多个脚本中引用,方便函数重用。
示例库函数:
cat myfuncs# my script functionsfunction addem {echo $(($1 + $2))}function multem {echo $(($1 * $2))}function divem {if [ $2 -ne 0 ]thenecho $(($1 / $2))elseecho -1fi}
和环境变量一样,shell 函数仅在定义它的 shell 会话内有效。在命令行中运行 myfuncs 脚本,shell 会创建一个新的 shell 运行它,之后在当前的 shell 中也无法使用它:
$ ./myfuncs$ addem 1 2bash: addem: command not found
source 命令会在当前 shell 上下文中执行命令,而不是创建一个新 shell。source 命令有一个快捷的别名,称作点操作符(dot operator)。
#!/bin/bash# 使用库函数# 这种方式不正确# ./myfunc# 使用这种方式执行. ./myfuncsresult=$(addem 10 15)echo "The result is $result"
在命令行上使用库函数
在命令行上创建函数:
$ function divem { echo $(($1 / $2));}$ divem 5 22$ function multem {> echo $(($1 * $2))> }$ multem 2 510
在 .bashrc 中定义函数,或者使用点操作符加载库函数。这样就可以在 shell 中是使用了。
