<5>. 函数
5.1 函数声明和定义
下面的程序中定义函数demo,向函数传递的所有参数表示为$*,第一个参数$1,第二个参数$2, 依次类推。
#!/bin/bash
function demo()
{
echo"all function args : $*"
echo"the first arg : $1"
echo"the second arg : $2"
echo"the third arg : $3"
}
#call the function
function demo()
{
echo"all function args : $*"
echo"the first arg : $1"
echo"the second arg : $2"
echo"the third arg : $3"
}
#call the function
demo -f foo bar
5.2 函数调用(函数参数)
shell中向函数传递参数是通过直接在函数调用后面添加参数完成,在函数中,传递的参数通过$1得到。
<6>. 脚本调试
6.1 万能的echo
shell的脚本调试是比较恶心的,这里仅仅是提供一些常规性的调试方法,最简单的就是使用echo函数打印出变量的值从而达到调试目的。
6.2 两个命令
shell脚本执行可以通过./shell-filename.sh的形式执行,另外的一种形式是通过bash ./shell-filename.sh的形式执行,同时在执行脚本时,可以使用-v或者是-x参数打印程序执行信息。
-v:默认打印shell读取的内容
-x:将命令“展开” 之后打印
例如对于下面的程序:
!/bin/sh
#fordebugshell script#
tot=`expr $1+$2`
echo$tot
如果使用-v选项,结果如下:
xuqiang@Ubuntu:~/shell$ sh -v ./debug.sh 2 5
#!/bin/sh
#
# for debug shell script
#
tot=`expr $1 + $2`
echo $tot
7
如果是使用-x选项的话,结果:
xuqiang@ubuntu:~/shell$ sh -x ./debug.sh 2 5
+ expr 2 + 5
+ tot=7
+ echo 7
7
<7>. 参考资料及shell代码下载
参考资料:http://www.freeos.com/guides/lsst/index.html
shell脚本 与 操作系统助教时的ppt下载
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2013年资料/2月/16日/Linux shell编程
