壳牌入门教程:壳牌函数详解
壳函数类似于外壳脚本,里面存放了一系列的指令,不过壳的函数存在于内存,而不是硬盘文件,所以速度很快,另外,外壳还能对函数进行预处理,所以函数的启动比脚本更快。
1,函数定义
1
2
3
4
|
function
函数名() {
语句
[
return
]
}
|
关键字函数表示定义一个函数,可以省略,其后是函数名,有时函数名后可以跟一个括号,符号“ { ”表示函数执行命令的入口,该符号也可以在函数名那一行,“ } ”表示函数体的结束,两个大括号之间是函数体。
语句部分可以是任意的壳牌命令,也可以调用其他的函数。
如果在函数中使用退出命令,可以退出整个脚本,通常情况,函数结束之后会返回调用函数的部分继续执行。
可以使用破解语句来中断函数的执行。
声明-f可以显示定义的函数清单
声明-F可以只显示定义的函数名
unset -f可以从Shell内存中删除函数
export -f将函数输出给Shell
另外,函数的定义可以放到的.bash_profile文件中,也可以放到使用函数的脚本中,还可以直接放到命令行中,还可以使用内部的未固化的命令删除函数。一旦用户注销,壳牌将不再保持这些函数。
2,函数的调用
函数调用的实例:
1
2
3
4
五
6
7
8
|
#!/bin/bash
function
show() {
echo
"hello , you are calling the function"
}
echo
"first time call the function"
show
echo
"second time call the function"
show
|
3,函数参数的传递
可以函数通过位置变量传递参数。例如
函数名参数1参数2参数3参数4
当函数执行时,$ 1对应参数1,其他依次类推。
实例:
1
2
3
4
五
6
7
8
|
#!/bin/bash
function
show() {
echo
"hello , you are calling the function $1"
}
echo
"first time call the function"
show first
echo
"second time call the function"
show second
|
4,函数的返回值
函数中的关键字“ return ”可以放到函数体的任意位置,通常用于返回某些值,Shell在执行到返回之后,就停止往下执行,返回到主程序的调用行,返回的返回值只能是0〜256之间的一个整数,返回值将保存到变量“ $?“中。
实例:
1
2
3
4
五
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/bash
function
abc() {
RESULT=`
expr
$1 \% 2`
#表示取余数
if
[ $RESULT –</code><code class="bash keyword">ne</code> <code class="bash plain">0 ] ; </code><code class="bash keyword">then</code></div><div class="line number5 index4 alt2"><code class="bash spaces"> </code><code class="bash keyword">return</code> <code class="bash plain">0</code></div><div class="line number6 index5 alt1"><code class="bash spaces"> </code><code class="bash keyword">else</code></div><div class="line number7 index6 alt2"><code class="bash spaces"> </code><code class="bash keyword">return</code> <code class="bash plain">1</code></div><div class="line number8 index7 alt1"><code class="bash spaces"> </code><code class="bash keyword">fi</code></div><div class="line number9 index8 alt2"><code class="bash plain">}</code></div><div class="line number10 index9 alt1"><code class="bash functions">echo</code> <code class="bash string">"Please enter a number who can devide by 2"</code></div><div class="line number11 index10 alt2"><code class="bash functions">read</code> <code class="bash plain">N</code></div><div class="line number12 index11 alt1"><code class="bash plain">abc $N
case
$?
in
0)
echo
"
yes
,it is”
;;
1)
echo
“no ,it isn’t”
;;
esac
|
在这里要注意参数传递了,上面读进来的数字,必须加上$符号才能传递给函数,我刚开始不知道是哪里错了,找了半天才知道是这里出错了。
5,函数的载入
如果函数在另外一个文件中,我们该怎么调用它呢?
这里就有一个方法。比如显示函数写在了function.sh里面了,我们就可以用源命令
1
2
|
source
function
.sh
show
|
这样就可以调用了。
如图6所示,函数的删除
用法:unset -f函数名
7,函数的变量作用域
默认情况下,变量具有全局作用域,如果想把它设置为局部作用域,可以在其前加入本地
例如:
1
|
local
a=
"hello"
|
使用局部变量,使得函数在执行完毕后,自动释放变量所占用的内存空间,从而减少系统资源的消耗,在运行大型的程序时,定义和使用局部变量尤为重要。
如图8所示,函数的嵌套
函数可以进行嵌套,实例:
1
2
3
4
五
6
7
8
9
10
11
12
13
14
15
|
#!/bin/bash
function
first() {
function
second() {
function
third() {
echo
"------this is third"
}
echo
"this is the second"
third
}
echo
"this is the first"
second
}
echo
"start..."
first
|
</div>
<div class="postDesc">posted @ <span id="post-date">2016-07-14 11:06</span> <a href="http://www.cnblogs.com/52php/">52php</a> 阅读(<span id="post_view_count">16138</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=5669675" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(5669675);return false;">收藏</a></div>
</ div>
<script type =“text / javascript”> var allowComments = true,cb_blogId = 110339,cb_entryId = 5669675,cb_blogApp = currentBlogApp,cb_blogUserGuid ='384d7c4b-b863-e111-aa3f-842b2b196315',cb_entryCreatedDate ='2016 / 7/14 11:06:00'; loadViewCount(cb_entryId); var cb_postType = 1; </ script>