linux,shell学习(六)

[size=large][b][align=center]shell函数[/align][/b][/size]
[b] 1.定义函数 4.函数文件
2.函数调用 5.载入和删除函数
3.参数传递 6.函数返回状态[/b]
[color=green]1.定义函数[/color]
语法:
[ function ] funname [()]
{
action;
[return int;]
}
说明:
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255
#!/bin/bash
#hello fun
function hello() {
echo "hello fun,today is `date`"
return 1
}

[color=green]2.函数调用[/color]
#!/bin/bash
#hello fun
function hello() {
echo "hello fun,today is `date`"
return 1
}
echo "exec funtion"
hello
echo "finish"

[color=green]3.参数传递[/color]
#!/bin/bash
#hello fun
function hello() {
echo "hello $1,today is `date`"
return 1
}
echo "exec funtion"
hello world
echo "finish"

[color=green]4.函数文件[/color]
#!/bin/bash
#hello fun
#source function
. hellofun
echo "now going to the function hello"
hello
echo "back from function hello"

function hello() {
echo "hello,today is `date`"
return 1
}

[color=green]5.函数的载入查看与删除函数[/color]
#!/bin/bash
#hello fun
#source function
. hellofun
set #[color=darkred]查看函数是否载入[/color]
#unset 删除函数
echo "now going to the function hello"
hello
echo "back from function hello"

function hello() {
echo "hello,today is `date`"
return 1
}

[color=green]6.函数返回状态[/color]
#!/bin/bash
#hello fun
#source function
. hellofun
echo "now going to the function hello"
hello
echo $?
echo "back from function hello"

function hello() {
echo "hello,today is `date`"
return 1
}

[b]注意:一般返回 [color=red]0[/color] 为正确 [color=red]1[/color] 为错误。
[color=green][size=large]shell函数没有返回值,返回的都是状态值[/size][/color]
[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值