- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
shell中的函数
Linux命令、编辑器、shell编程实例大全----第十九章函数
1. 函数简介 1
1.return返回上一个命令的退出状态或是给定值 1
2.exit退出整个脚本 1
3.函数中使用break语句中断函数执行 1
4.declare -f 显示定义的函数清单。 1
5.exprot -f 将函数导出给shell 2
6.unset -f 从shell内存中删除函数。 2
2. Shell中的调用顺序 2
3. 函数的传参 2
4. 函数的返回值 4
5.将函数载入到内存 5
6. 删除函数 7
7. 函数的作用域 7
8.最简单的递归调用 7
函数简介
【function】函数名
{
命令表
【Return】
}
说明:
1.return返回上一个命令的退出状态或是给定值
2.exit退出整个脚本
root@ubuntu-vm:/home/2014-12-15# cat exit.sh
#! /bin/bash
function test
{
echo function calling ...!
exit
}
echo call function start
test
echo call function end
root@ubuntu-vm:/home/2014-12-15# ./exit.sh
call function start
function calling ...!
root@ubuntu-vm:/home/2014-12-15#
3.函数中使用break语句中断函数执行
4.declare -f 显示定义的函数清单。
root@ubuntu-vm:/home/2014-12-15# declare -f
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1;
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found/command-not-found -- $1;
return $?;
else
printf %s: command not found\n $1 12;
return 127;
fi;
fi
}
root@ubuntu-vm:/home/2014-12-15# declare -F
declare -f command_not_found_handle
root@ubuntu-vm:/home/2014-12-15#
# -x判断文件存在且可执行
5.exprot -f 将函数导出给shell
6.unset -f 从shell内存中删除函数。
Shell中的调用顺序
shell在执行一个别名、函数、内部命令、基于磁盘的可执行文件(执行顺序也是如此)
root@ubuntu-vm:/home/2014-12-15# function mydate
{
echo start ...
date
echo end ...
}
root@ubuntu-vm:/home/2014-12-15# mydate
start ...
Mon Dec 15 19:16:29 CST 2014
end ...
root@ubuntu-vm:/home/2014-12-15# declare -f
mydate ()
{
echo start ...;
date;
echo end ...
}
root@ubuntu-vm:/home/2014-12-15#
函数的传参
实例1.
root@ubuntu-vm:/home/2014-12-15# cat args.sh
#! /bin/bash
#function:function argsment
function argss
{
echo $1 $2 $3
echo $a $b $c
return
}
a=111
b=222
c=333
argss a b c
root@ubuntu-vm:/home/2014-12-15# ./args.sh
a b c
111 2
文档评论(0)