<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; shell 函数</title>
	<atom:link href="http://www.liguosong.com/tag/shell-%e5%87%bd%e6%95%b0/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liguosong.com</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2018 01:02:19 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>Shell 函数</title>
		<link>http://www.liguosong.com/2014/01/01/shell-%e5%87%bd%e6%95%b0/</link>
		<comments>http://www.liguosong.com/2014/01/01/shell-%e5%87%bd%e6%95%b0/#comments</comments>
		<pubDate>Wed, 01 Jan 2014 10:46:54 +0000</pubDate>
		<dc:creator>lgs</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shell 函数]]></category>

		<guid isPermaLink="false">http://www.liguosong.com/?p=220</guid>
		<description><![CDATA[在开始编写复杂的shell脚本时，有时候你会发现在编写很多重复的代码。bash shell支持用户定义的函数。 [...]]]></description>
				<content:encoded><![CDATA[<p>在开始编写复杂的shell脚本时，有时候你会发现在编写很多重复的代码。bash shell支持用户定义的函数。可以将shell脚本的代码进行封装。方便以后多次使用和维护。<span id="more-220"></span></p>
<p>有两种方式可以创建bash shell的函数：<br />
方式一：采用关键字function，后面跟着函数名称。例如：</p>
<pre class="brush:bash">#!/bin/bash

function hello {
	echo This is hello function.
}

hello</pre>
<p><em>输出结果：<br />
This is hello function.</em></p>
<p><strong> 注意： 方法与后面的大括号要有空格，不可以写成 Hello{  这样是错误的。</strong></p>
<p>方式二：不实用function关键字。直接是方法的名字。例如：</p>
<pre class="brush:bash">#!/bin/bash

hello() {
	echo This is hello function.
}

hello</pre>
<p><em>输出结果：<br />
This is hello function.</em></p>
<p><strong>第二种方法，函数名和后面的小括号和大括号可以有空格也可以没有。不过建议在大括号写一个空格。</strong></p>
<p><strong>注意：再调用函数的时候，函数定义必须在调用之前定义。<br />
如果在同一个shell中重复定义了一个函数，不会产生任何错误。后续使用该函数，都会使用第二个定义的函数。例如：</strong></p>
<pre class="brush:bash">#!/bin/bash

hello() {
	echo This is hello function.
}

hello

hello() {
	echo This is a repeat of the hello function.
}

hello</pre>
<p><em>输出结果：<br />
</em>This is hello function.<br />
This is a repeat of the hello function.</p>
<p>这只是简单的函数，没有参数，没有返回值。<br />
继续研究一下bash shell 如何获取函数退出状态码。可以说有三种返回值：<br />
方式一：<br />
默认状态下，函数的退出状态码是函数中执行最后一条命令的退出状态码，可以用$?获取状态码。例如：</p>
<pre class="brush:bash">testReturn() {
	echo Hello
}

testReturn

echo The exit status is $?</pre>
<p>输出结果：<br />
0</p>
<p>方式二：使用return。return命令允许指定一个数值作为函数的退出状态码。</p>
<pre class="brush:bash">testReturn() {
	return 10
}

testReturn
echo The exit status is $?</pre>
<p>输出结果：10<br />
注意：退出状态码必须在0-255之间。要是返回较大的数值或者字符串，必须使用下面的方法了。</p>
<p>方法三：将函数的输出保存到shell变量中。</p>
<pre class="brush:bash">#!/bin/bash

testReturn() {
	echo Hello
}

RESULT=`testReturn`
echo $RESULT</pre>
<p>输出结果：Hello</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liguosong.com/2014/01/01/shell-%e5%87%bd%e6%95%b0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
