<?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>Remember the code? &#187; shell script</title>
	<atom:link href="http://code.neox.net/tag/shell-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.neox.net</link>
	<description>a place where I store my secret powerful coding snippets, or just to remember how to do things</description>
	<lastBuildDate>Fri, 19 Jun 2009 11:36:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>shell script : using sub routine</title>
		<link>http://code.neox.net/2008/05/14/shell-script-using-sub-routine/</link>
		<comments>http://code.neox.net/2008/05/14/shell-script-using-sub-routine/#comments</comments>
		<pubDate>Thu, 15 May 2008 00:15:17 +0000</pubDate>
		<dc:creator>HanaDaddy</dc:creator>
				<category><![CDATA[shell]]></category>
		<category><![CDATA[shell script]]></category>

		<guid isPermaLink="false">http://coding.neox.net/?p=8</guid>
		<description><![CDATA[I do lots of unix shell scripting. It&#8217;s easy and I can get things done without hassle.
Okay, now this one is maybe easy. But I would like to call a subroutine defined in the library script file, let&#8217;s say &#8220;lib.inc&#8221;.
lib.inc

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
&#160;
# This is our real simple sub routine returns 0 value when it is happy
is_happy &#40;&#41;
&#123;
 [...]]]></description>
			<content:encoded><![CDATA[<p>I do lots of unix shell scripting. It&#8217;s easy and I can get things done without hassle.<br />
Okay, now this one is maybe easy. But I would like to call a subroutine defined in the library script file, let&#8217;s say &#8220;lib.inc&#8221;.</p>
<p><strong>lib.inc</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This is our real simple sub routine returns 0 value when it is happy</span>
is_happy <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
     <span style="color: #808080; font-style: italic;">#First arg $1, Second arg  $2, third $3 ... so on.</span>
     <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> = <span style="color: #ff0000;">&quot;:)&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span>
     	<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
     <span style="color: #000000; font-weight: bold;">else</span>
     	<span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">1</span>
     <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>You can include the lib.inc by using &#8220;.&#8221; notation.<br />
When you call the sub routine , call it just like you execute an external command. And the return value can be obtained using <code>$?</code>. Just like getting the return value of external command after execution.</p>
<p>This is main script calling the <code>is_happy()</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/sh</span>
<span style="color: #808080; font-style: italic;">#including the lib.inc file</span>
. lib.inc
&nbsp;
<span style="color: #808080; font-style: italic;">#calling subroutine</span>
is_happy <span style="color: #ff0000;">&quot;:)&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> -eq <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;I am happy!&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;No I am not happy!&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p>That was easy right? There will be more fun on shell scripting at my Coding Haven soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.neox.net/2008/05/14/shell-script-using-sub-routine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
