一。函数定义
1.shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数
2.格式
函数名
{命令1
...}
function 函数名()
{}
3.函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中
#!/bin/bash
#hellofun
function hello()
{echo "hello ,today is `date`"
return 1}
二。函数调用
#!/bin/bash
#func
function hello()
{echo "hello,today is `date`"
return 1}
echo "now going to function hello"
hello
echo "back from the function"
三。参数传递
向函数传递参数就像在脚本中使用位置变量$1,$2,...$9
#!/bin/bash
#func
function hello()
{echo "hello,$1,today is `date`"
return 1}
echo "now going to function hello"
hello china
echo "back from the function"
四。函数文件
#!/bin/bash
#func
#source function
.hellofun
echo "now going to function hello"
hello
echo "back from the function"
#!/bin/bash
#hellofun
function hello()
{echo "hello,today is `date`"
return 1}
more /etc/init.d/
五。检查载入函数和删除函数
查看载入函数set
#!/bin/bash
#func
#source function
.hellofun
set
echo "now going to function hello"
hello
echo "back from the function"
#!/bin/bash
#hellofun
function hello()
{echo "hello,today is `date`"
return 1}
删除函数unset
#!/bin/bash
#func
#source function
.hellofun
set
unset hello
echo "now going to function hello"
hello
echo "back from the function"
#!/bin/bash
#hellofun
function hello()
{echo "hello,today is `date`"
return 1}
六。函数返回状态值
#!/bin/bash
#func
#source function
.hellofun
echo "now going to function hello"
hello
echo $?
echo "back from the function"
#!/bin/bash
#hellofun
function hello()
{echo "hello,today is `date`"
return 1}
阅读(783) | 评论(0) | 转发(0) |