shell编程学习笔记四

shell函数


定义函数


定义函数的格式
function 函数名(){
  命令
}






在脚本中使用函数
示例代码如下:
hello(){
echo "hello there today date is `date`"
}


在脚本中使用函数
示例代码如下:
#!/bin/sh
#test20
#在脚本中使用函数
hello()
{
echo "Hello there today's data is `date`"
}
echo "now going to the function hello"
hello


echo "back from function"
执行结果:
[retacn@localhost tmp]$ ./test20
now going to the function hello
Hello there today's data is 2013年 01月 04日 星期五 03:13:47 PST
back from function


在函数文件中使用函数
创建函数文件
#!/bin/sh
#test21
#函数文件
findit(){
 if [ $# -lt l ]; then
 echo "usage :findit file"
 return 1
 fi
 find / -name $l -print




编辑执行shell函数


示例代码如下:


#!/bin/sh
#function.main
#查找文件
findit(){
if [ $# -lt l ];then
  echo "usage: findit file"
  return 1
fi
for loop
do
 find / -name $loop -print
done
}




定位函数
[root@localhost tmp]# . /function.main 


set命令查看函数是否被载入,


执行函数

posted @ 2013-01-04 20:34  retacn_yue  阅读(103)  评论(0编辑  收藏  举报