<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>朱文昊 Albert Zhu &#187; Bash</title>
	<atom:link href="http://zhuwenhao.com/category/%e6%8a%80%e6%9c%af/%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e8%af%ad%e8%a8%80/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://zhuwenhao.com</link>
	<description>朱文昊的中文博客－－专注技术，向往自由</description>
	<lastBuildDate>Thu, 10 May 2012 07:20:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bash scripting Tutorial</title>
		<link>http://zhuwenhao.com/339/%e6%8a%80%e6%9c%af/%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e8%af%ad%e8%a8%80/bash/bash-scripting-tutorial/</link>
		<comments>http://zhuwenhao.com/339/%e6%8a%80%e6%9c%af/%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e8%af%ad%e8%a8%80/bash/bash-scripting-tutorial/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 09:31:59 +0000</pubDate>
		<dc:creator>朱文昊 Albert Zhu</dc:creator>
				<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://zhuwenhao.com/339/%e6%8a%80%e6%9c%af/%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e8%af%ad%e8%a8%80/bash/bash-scripting-tutorial/</guid>
		<description><![CDATA[This bash script tutorial assumes no previous knowledge of bash scripting.As you will soon discover in this quick comprehensive bash scripting guide, learning the bash shell scripting is very easy task. Lets begin this bash scripting tutorial with a simple &#34;Hello World&#34; script. Let&#8217;s start with  [...]]]></description>
			<content:encoded><![CDATA[<p>This bash script tutorial assumes no previous knowledge of bash scripting.As you will soon discover in this quick comprehensive bash scripting guide, learning the bash shell scripting is very easy task. Lets begin this bash scripting tutorial with a simple &quot;Hello World&quot; script. Let&#8217;s start with Learning the bash Shell: Unix Shell Programming </p>
<h3>1. Hello World Bash Shell Script</h3>
<p>First you need to find out where is your bash interpreter located. Enter the following into your command line: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code49'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33949"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code49"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">bash</span></pre></td></tr></table></div>

<p><img title="Locate a Bash Interpreter" border="0" alt="bash interpreter &#13;&#10;location: /bin/bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/c9eeef568dc6a74c06196739cdd71f00.gif" width="188" height="43" /></p>
<p>Open up you favorite text editor and a create file called hello_world.sh. Insert the following lines to a file: </p>
<p>NOTE:Every bash shell script in this tutorial starts with <b>shebang:&quot;#!&quot;</b> which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash. </p>
<p>Here is our first bash shell script example: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code50'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33950"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p339code50"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># declare STRING variable</span>
<span style="color: #007800;">STRING</span>=<span style="color: #ff0000;">&quot;Hello World&quot;</span>
<span style="color: #666666; font-style: italic;">#print variable on a screen</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$STRING</span></pre></td></tr></table></div>

<p>Navigate to a directory where your hello_world.sh is located and make the file executable: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code51'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33951"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code51"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x hello_world.sh</pre></td></tr></table></div>

<p><img title="Make bash shell script executable" border="0" alt="Make bash shell &#13;&#10;script executable" src="http://zhuwenhao.com/wp-content/uploads/HLIC/0a2c82757f4f905a489385556026db66.gif" width="263" height="30" /></p>
<p>Now you are ready to execute your first bash script: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code52'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33952"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code52"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>hello_world.sh</pre></td></tr></table></div>

<p><img title="Example of simple bash shell script" border="0" alt="Example of simple bash shell &#13;&#10;script" src="http://zhuwenhao.com/wp-content/uploads/HLIC/7ff02785e875790a94eb50726f4a8622.gif" width="214" height="43" /></p>
<h3>2. Simple Backup bash shell script</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code53'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33953"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p339code53"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-czf</span> myhome_directory.tar.gz <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>linuxconfig</pre></td></tr></table></div>

<p><img title="Simple Backup bash script" border="0" alt="Simple &#13;&#10;Backup bash script" src="http://zhuwenhao.com/wp-content/uploads/HLIC/b477d46aadf122af534ad4d83fbc22b8.gif" /></p>
<h3>3. Variables</h3>
<p>In this example we declare simple bash variable and print it on the screen ( stdout ) with echo command. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code54'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33954"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p339code54"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
 <span style="color: #007800;">STRING</span>=<span style="color: #ff0000;">&quot;HELLO WORLD!!!&quot;</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$STRING</span></pre></td></tr></table></div>

<p><img title="Bash string Variables in bash &#13;&#10;script" border="0" alt="Bash &#13;&#10;string Variables in bash script" src="http://zhuwenhao.com/wp-content/uploads/HLIC/ae5462d925fe223bf74b9d6a8bd084da.gif" width="484" height="43" /></p>
<p>Your backup script and variables: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code55'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33955"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p339code55"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
 <span style="color: #007800;">OF</span>=myhome_directory_$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.gz
 <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-czf</span> <span style="color: #007800;">$OF</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>linuxconfig</pre></td></tr></table></div>

<p><img title="Bash backup Script with bash &#13;&#10;Variables" border="0" alt="Bash &#13;&#10;backup Script with bash Variables" src="http://zhuwenhao.com/wp-content/uploads/HLIC/0492f1b253e3206e74a27d801ab6df8e.gif" width="484" height="69" /></p>
<h4>3.1. Global vs. Local variables</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code56'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33956"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p339code56"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Define bash global variable</span>
<span style="color: #666666; font-style: italic;">#This variable is global and can be used anywhere in this bash script</span>
<span style="color: #007800;">VAR</span>=<span style="color: #ff0000;">&quot;global variable&quot;</span>
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;">#Define bash local variable</span>
<span style="color: #666666; font-style: italic;">#This variable is local to bash function only</span>
<span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">VAR</span>=<span style="color: #ff0000;">&quot;local variable&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span>
<span style="color: #c20cb9; font-weight: bold;">bash</span>
<span style="color: #666666; font-style: italic;"># Note the bash global variable did not change</span>
<span style="color: #666666; font-style: italic;"># &quot;local&quot; is bash reserved word</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span></pre></td></tr></table></div>

<p><img title="Global vs. &#13;&#10;Local Bash variables in bash script" border="0" alt="Global vs. Local Bash variables in bash script" src="http://zhuwenhao.com/wp-content/uploads/HLIC/5e6e695afe1319820dfbcfbd385c93a4.gif" width="190" height="69" /></p>
<h3>4. Passing arguments to the bash script</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code57'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33957"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p339code57"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Define bash global variable</span>
<span style="color: #666666; font-style: italic;">#This variable is global and can be used anywhere in this bash script</span>
<span style="color: #007800;">VAR</span>=<span style="color: #ff0000;">&quot;global variable&quot;</span>
<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #c20cb9; font-weight: bold;">bash</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;">#Define bash local variable</span>
<span style="color: #666666; font-style: italic;">#This variable is local to bash function only</span>
<span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #007800;">VAR</span>=<span style="color: #ff0000;">&quot;local variable&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span>
<span style="color: #c20cb9; font-weight: bold;">bash</span>
<span style="color: #666666; font-style: italic;"># Note the bash global variable did not change</span>
<span style="color: #666666; font-style: italic;"># &quot;local&quot; is bash reserved word</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code58'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33958"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code58"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>arguments.sh Bash Scripting Tutorial</pre></td></tr></table></div>

<p><img title="Passing arguments to the bash &#13;&#10;script" border="0" alt="Passing &#13;&#10;arguments to the bash script" src="http://zhuwenhao.com/wp-content/uploads/HLIC/77fc446d6486a9fd3796be51f3967507.gif" width="472" height="82" /></p>
<h3>5. Executing shell commands with bash</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code59'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33959"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p339code59"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># use backticks &quot; ` ` &quot; to execute shell command</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">uname</span> -o<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #666666; font-style: italic;"># executing bash command without backticks</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #c20cb9; font-weight: bold;">uname</span> <span style="color: #660033;">-o</span></pre></td></tr></table></div>

<p><img title="Executing shell commands&#13;&#10; with bash" border="0" alt="Executing shell commands with bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/7d9ae9ca9467d410266bd2112edcc0d9.gif" width="220" height="82" /></p>
<h3>6. Reading User Input</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code60'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33960"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p339code60"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;Hi, please type the word: \c &quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span>  word
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The word you entered is: <span style="color: #007800;">$word</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;Can you please enter two words? &quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> word1 word2
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Here is your input: <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$word1</span><span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$word2</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;How do you feel about bash scripting? &quot;</span>
<span style="color: #666666; font-style: italic;"># read command now stores a reply into the default build-in variable $REPLY</span>
<span style="color: #c20cb9; font-weight: bold;">read</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You said <span style="color: #007800;">$REPLY</span>, I'm glad to hear that! &quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;What are your favorite colours ? &quot;</span>
<span style="color: #666666; font-style: italic;"># -a makes read command to read into an array</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-a</span> colours
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;My favorite colours are also <span style="color: #007800;">${colours[0]}</span>, <span style="color: #007800;">${colours[1]}</span> and <span style="color: #007800;">${colours[2]}</span>:-)&quot;</span></pre></td></tr></table></div>

<p><img title="Reading User Input with bash" border="0" alt="Reading User &#13;&#10;Input with bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/08106ad44d62d1a6419ad99bb577ab60.gif" width="322" height="173" /></p>
<h3>7. Bash Trap Command</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code61'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33961"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p339code61"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># bash trap command</span>
<span style="color: #7a0874; font-weight: bold;">trap</span> bashtrap INT
<span style="color: #666666; font-style: italic;"># bash clear screen command</span>
<span style="color: #c20cb9; font-weight: bold;">clear</span>;
<span style="color: #666666; font-style: italic;"># bash trap function is executed when CTRL-C is pressed:</span>
<span style="color: #666666; font-style: italic;"># bash prints message =&gt; Executing bash trap subrutine !</span>
bashtrap<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: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;CTRL+C Detected !...executing bash trap !&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #666666; font-style: italic;"># for loop from 1/10 to 10/10</span>
<span style="color: #000000; font-weight: bold;">for</span> a <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">1</span> <span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$a</span>/10 to Exit.&quot;</span> 
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>;
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Exit Bash Trap Example!!!&quot;</span></pre></td></tr></table></div>

<h3>8. Arrays</h3>
<h4>8.1. Declare simple bash array</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code62'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33962"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p339code62"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Declare array with 4 elements</span>
<span style="color: #007800;">ARRAY</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #ff0000;">'Debian Linux'</span> <span style="color: #ff0000;">'Redhat Linux'</span> Ubuntu Linux <span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #666666; font-style: italic;"># get number of elements in the array</span>
<span style="color: #007800;">ELEMENTS</span>=<span style="color: #800000;">${#ARRAY[@]}</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># echo each element in array </span>
<span style="color: #666666; font-style: italic;"># for loop</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #007800;">i</span>=<span style="color: #000000;">0</span>;i<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #007800;">$ELEMENTS</span>;i++<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${ARRAY[${i}</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Declare simple bash array" border="0" alt="Declare simple bash array" src="http://zhuwenhao.com/wp-content/uploads/HLIC/1ccb908a13e9bb40850eaeeb2e038555.gif" width="154" height="82" /></p>
<h4>8.2. Read file into bash array</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code63'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33963"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p339code63"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Declare array</span>
 <span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-a</span> ARRAY
<span style="color: #666666; font-style: italic;">#Open file for reading to array</span>
<span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">10</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">count</span>=<span style="color: #000000;">0</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> LINE <span style="color: #000000; font-weight: bold;">&lt;&amp;</span><span style="color: #000000;">10</span>; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
    ARRAY<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">$count</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>=<span style="color: #007800;">$LINE</span>
    <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>count++<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> Number of elements: <span style="color: #800000;">${#ARRAY[@]}</span>
<span style="color: #666666; font-style: italic;"># echo array's content</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${ARRAY[@]}</span>
<span style="color: #666666; font-style: italic;"># close file </span>
<span style="color: #7a0874; font-weight: bold;">exec</span> <span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span>-</pre></td></tr></table></div>

<p><img title="Read file into bash array" border="0" alt="Read file into bash&#13;&#10; array" src="http://zhuwenhao.com/wp-content/uploads/HLIC/437c1b0937f439c86352ea1d879f4872.gif" width="232" height="121" /></p>
<h3>9. Bash if / else / fi statements</h3>
<h4>9.1. Simple Bash if/else statement</h4>
<p>Please note the spacing inside the [ and ] brackets! Without the spaces, it won&#8217;t work! </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code64'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33964"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p339code64"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">directory</span>=<span style="color: #ff0000;">&quot;./BashScripting&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash check if directory exists</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$directory</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;Directory exists&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;Directory does not exists&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash if else fi statement" border="0" alt="Bash if else fi &#13;&#10;statement" src="http://zhuwenhao.com/wp-content/uploads/HLIC/112551431e9bc88ffac74229de5b517b.gif" width="232" height="82" /></p>
<h4>9.2. Nested if/else</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code65'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33965"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p339code65"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Declare variable choice and assign value 4</span>
<span style="color: #007800;">choice</span>=<span style="color: #000000;">4</span>
<span style="color: #666666; font-style: italic;"># Print to stdout</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;1. Bash&quot;</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;2. Scripting&quot;</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;3. Tutorial&quot;</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Please choose a word [1,2 or 3]? &quot;</span>
<span style="color: #666666; font-style: italic;"># Loop while the variable choice is equal 4</span>
<span style="color: #666666; font-style: italic;"># bash while loop</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$choice</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">4</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># read user input</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> choice
<span style="color: #666666; font-style: italic;"># bash nested if/else</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$choice</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span>
&nbsp;
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You have chosen word: Bash&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">else</span>                   
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$choice</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">2</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;You have chosen word: Scripting&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$choice</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">3</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;You have chosen word: Tutorial&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;Please make a choice between 1-3 !&quot;</span>
                        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;1. Bash&quot;</span>
                        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;2. Scripting&quot;</span>
                        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;3. Tutorial&quot;</span>
                        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Please choose a word [1,2 or 3]? &quot;</span>
                        <span style="color: #007800;">choice</span>=<span style="color: #000000;">4</span>
                <span style="color: #000000; font-weight: bold;">fi</span>   
        <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Nested Bash if else statement" border="0" alt="Nested Bash if&#13;&#10; else statement" src="http://zhuwenhao.com/wp-content/uploads/HLIC/97f0aa3e6d9cfdf75b13ae8a247d8c05.gif" width="232" height="160" /></p>
<h3>10. Bash Comparisons</h3>
<h4>10.1. Arithmetic Comparisons</h4>
<table border="1" cellspacing="0" cellpadding="2" width="100">
<tbody>
<tr>
<td valign="top" width="50">-lt</td>
<td valign="top" width="50">&lt;</td>
</tr>
<tr>
<td valign="top" width="50">-gt </td>
<td valign="top" width="50">&gt;</td>
</tr>
<tr>
<td valign="top" width="50">-le</td>
<td valign="top" width="50">&lt;=</td>
</tr>
<tr>
<td valign="top" width="50">-ge</td>
<td valign="top" width="50">&gt;=</td>
</tr>
<tr>
<td valign="top" width="50">-eq</td>
<td valign="top" width="50">==</td>
</tr>
<tr>
<td valign="top" width="50">-ne </td>
<td valign="top" width="50">!=</td>
</tr>
</tbody>
</table>
<p>&#160;</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code66'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33966"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p339code66"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># declare integers</span>
<span style="color: #007800;">NUM1</span>=<span style="color: #000000;">2</span>
<span style="color: #007800;">NUM2</span>=<span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM1</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$NUM2</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;Both Values are equal&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;Values are NOT equal&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash Arithmetic Comparisons" border="0" alt="Bash &#13;&#10;Arithmetic Comparisons" src="http://zhuwenhao.com/wp-content/uploads/HLIC/b721db3346dc7dbf37f67d0f85fbbc84.gif" width="484" height="43" /></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code67'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33967"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p339code67"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># declare integers</span>
<span style="color: #007800;">NUM1</span>=<span style="color: #000000;">2</span>
<span style="color: #007800;">NUM2</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM1</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$NUM2</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;Both Values are equal&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;Values are NOT equal&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash Arithmetic &#13;&#10;Comparisons - values are NOT equal" border="0" alt="Bash &#13;&#10;Arithmetic Comparisons - values are NOT equal" src="http://zhuwenhao.com/wp-content/uploads/HLIC/fbe06f5314e7327ab178ad2131382c59.gif" width="484" height="43" /></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code68'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33968"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p339code68"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># declare integers</span>
<span style="color: #007800;">NUM1</span>=<span style="color: #000000;">2</span>
<span style="color: #007800;">NUM2</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">if</span>   <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM1</span> <span style="color: #660033;">-eq</span> <span style="color: #007800;">$NUM2</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;Both Values are equal&quot;</span>
<span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$NUM1</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$NUM2</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;NUM1 is greater then NUM2&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;NUM2 is greater then NUM1&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash Arithmetic &#13;&#10;Comparisons - greater then" border="0" alt="Bash Arithmetic Comparisons - greater then" src="http://zhuwenhao.com/wp-content/uploads/HLIC/04627a98974dd894a6485dac8504529b.gif" width="370" height="43" /></p>
<h4>10.2. String Comparisons</h4>
<table border="1" cellspacing="0" cellpadding="2" width="244">
<tbody>
<tr>
<td valign="top" width="64">= </td>
<td valign="top" width="178">equal</td>
</tr>
<tr>
<td valign="top" width="64">!= </td>
<td valign="top" width="178">not equal</td>
</tr>
<tr>
<td valign="top" width="64">&lt; </td>
<td valign="top" width="178">less then</td>
</tr>
<tr>
<td valign="top" width="64">&gt;</td>
<td valign="top" width="178">greater then</td>
</tr>
<tr>
<td valign="top" width="64">-n s1</td>
<td valign="top" width="178">string s1 is not empty</td>
</tr>
<tr>
<td valign="top" width="64">-z s1</td>
<td valign="top" width="178">string s1 is empty</td>
</tr>
</tbody>
</table>
<p>&#160;</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code69'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33969"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p339code69"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Declare string S1</span>
<span style="color: #007800;">S1</span>=<span style="color: #ff0000;">&quot;Bash&quot;</span>
<span style="color: #666666; font-style: italic;">#Declare string S2</span>
<span style="color: #007800;">S2</span>=<span style="color: #ff0000;">&quot;Scripting&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$S1</span> = <span style="color: #007800;">$S2</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;Both Strings are equal&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;Strings are NOT equal&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash String &#13;&#10;Comparisons - values are NOT equal" border="0" alt="Bash String Comparisons - values are NOT equal" src="http://zhuwenhao.com/wp-content/uploads/HLIC/f1f6b5b83ab442790d6a0cb6353475a4.gif" width="370" height="43" /></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code70'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33970"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p339code70"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#Declare string S1</span>
<span style="color: #007800;">S1</span>=<span style="color: #ff0000;">&quot;Bash&quot;</span>
<span style="color: #666666; font-style: italic;">#Declare string S2</span>
<span style="color: #007800;">S2</span>=<span style="color: #ff0000;">&quot;Bash&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$S1</span> = <span style="color: #007800;">$S2</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;Both Strings are equal&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;Strings are NOT equal&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Locate a Bash Interpreter" border="0" alt="bash &#13;&#10;interpreter location: /bin/bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/fd94227f1a2a864b5317cdef6bec2201.gif" width="370" height="43" /></p>
<h3>11. Bash File Testing</h3>
<table border="1" cellspacing="0" cellpadding="2" width="539">
<tbody>
<tr>
<td valign="top" width="134">-b filename</td>
<td valign="top" width="403">Block special file</td>
</tr>
<tr>
<td valign="top" width="134">-c filename</td>
<td valign="top" width="403">Special character file</td>
</tr>
<tr>
<td valign="top" width="134">-d directoryname</td>
<td valign="top" width="403">Check for directory existence</td>
</tr>
<tr>
<td valign="top" width="134">-e filename</td>
<td valign="top" width="403">Check for file existence</td>
</tr>
<tr>
<td valign="top" width="134">-f filename</td>
<td valign="top" width="403">Check for regular file existence not a directory</td>
</tr>
<tr>
<td valign="top" width="134">-G filename</td>
<td valign="top" width="403">Check if file exists and is owned by effective group ID.</td>
</tr>
<tr>
<td valign="top" width="134">-g filename</td>
<td valign="top" width="403">true if file exists and is set-group-id.</td>
</tr>
<tr>
<td valign="top" width="134">-k filename</td>
<td valign="top" width="403">Sticky bit</td>
</tr>
<tr>
<td valign="top" width="134">-L filename</td>
<td valign="top" width="403">Symbolic link</td>
</tr>
<tr>
<td valign="top" width="134">-O filename</td>
<td valign="top" width="403">True if file exists and is owned by the effective user id.</td>
</tr>
<tr>
<td valign="top" width="134">-r filename</td>
<td valign="top" width="403">Check if file is a readable</td>
</tr>
<tr>
<td valign="top" width="134">-S filename</td>
<td valign="top" width="403">Check if file is socket</td>
</tr>
<tr>
<td valign="top" width="134">-s filename</td>
<td valign="top" width="403">Check if file is nonzero size</td>
</tr>
<tr>
<td valign="top" width="134">-u filename</td>
<td valign="top" width="403">Check if file set-ser-id bit is set</td>
</tr>
<tr>
<td valign="top" width="134">-w filename</td>
<td valign="top" width="403">Check if file is writable</td>
</tr>
<tr>
<td valign="top" width="134">-x filename</td>
<td valign="top" width="403">Check if file is executable</td>
</tr>
</tbody>
</table>
<p>&#160;</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code71'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33971"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p339code71"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">file</span>=<span style="color: #ff0000;">&quot;./file&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$file</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;File exists&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;File does not exists&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div>

<p><img title="Bash File Testing - File does&#13;&#10; not exist" border="0" alt="Bash &#13;&#10;File Testing - File does not exist" src="http://zhuwenhao.com/wp-content/uploads/HLIC/17f4ed69b54e9c09978b607eb523ed99.gif" width="370" height="43" /> <img title="Bash File Testing - File exists" border="0" alt="Bash File &#13;&#10;Testing - File exists" src="http://zhuwenhao.com/wp-content/uploads/HLIC/253cb30c2c29322e7d35facb8e47c124.gif" width="370" height="82" /></p>
<p>Similarly for example we can use while loop to check if file does not exists. This script will sleep until file does exists. Note bash negator &quot;!&quot; which negates the -e option. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code72'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33972"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p339code72"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> myfile <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #666666; font-style: italic;"># Sleep until file does exists/is created</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<h3>12. Loops</h3>
<h4>12.1. Bash for loop</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code73'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33973"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p339code73"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash for loop</span>
<span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$f</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Running for loop from bash shell command line: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code74'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33974"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code74"><pre class="bash" style="font-family:monospace;">$ <span style="color: #000000; font-weight: bold;">for</span> f <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$f</span>; <span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Bash for &#13;&#10;loop" border="0" alt="Bash for loop" src="http://zhuwenhao.com/wp-content/uploads/HLIC/3d23e6cc5766905da4d6e945d4193a5e.gif" width="484" height="173" /></p>
<h4>12.2. Bash while loop</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code75'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33975"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p339code75"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">COUNT</span>=<span style="color: #000000;">6</span>
<span style="color: #666666; font-style: italic;"># bash while loop</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$COUNT</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> Value of count is: <span style="color: #007800;">$COUNT</span>
	<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">COUNT</span>=COUNT-<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Bash &#13;&#10;while loop" border="0" alt="Bash while loop" src="http://zhuwenhao.com/wp-content/uploads/HLIC/6e00adae0a4e0d25b438b0352a2fae8c.gif" width="484" height="108" /></p>
<h4>12.3. Bash until loop</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code76'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33976"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p339code76"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">COUNT</span>=<span style="color: #000000;">0</span>
<span style="color: #666666; font-style: italic;"># bash until loop</span>
<span style="color: #000000; font-weight: bold;">until</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$COUNT</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">5</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Value of count is: <span style="color: #007800;">$COUNT</span>
        <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">COUNT</span>=COUNT+<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Bash &#13;&#10;until loop" border="0" alt="Bash until loop" src="http://zhuwenhao.com/wp-content/uploads/HLIC/0481db366dc78dae0a33bda6c03fe6ed.gif" width="484" height="108" /></p>
<h4>12.4. Control bash loop with </h4>
<p>Here is a example of while loop controlled by standard input. Until the redirection chain from STDOUT to STDIN to the read command exists the while loop continues. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code77'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33977"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p339code77"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># This bash script will locate and replace spaces</span>
<span style="color: #666666; font-style: italic;"># in the filenames</span>
<span style="color: #007800;">DIR</span>=<span style="color: #ff0000;">&quot;.&quot;</span>
<span style="color: #666666; font-style: italic;"># Controlling a loop with bash read command by redirecting STDOUT as</span>
<span style="color: #666666; font-style: italic;"># a STDIN to while loop</span>
<span style="color: #666666; font-style: italic;"># find will not truncate filenames containing spaces</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #007800;">$DIR</span> <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #c20cb9; font-weight: bold;">file</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #666666; font-style: italic;"># using POSIX class [:space:] to find space in the filename</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span> = <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>:space:<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">*</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #666666; font-style: italic;"># substitute space with &quot;_&quot; character and consequently rename the file</span>
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$file</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tr</span> <span style="color: #ff0000;">' '</span> <span style="color: #ff0000;">'_'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">fi</span>;
<span style="color: #666666; font-style: italic;"># end of while loop</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Bash script to replace&#13;&#10; spaces in the filenames with _" border="0" alt="Bash script &#13;&#10;to replace spaces in the filenames with _" src="http://zhuwenhao.com/wp-content/uploads/HLIC/7c098d41f4f65fc2ce845a30f382549c.gif" width="442" height="160" /></p>
<h3>13. Bash Functions</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code78'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33978"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p339code78"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">!/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span>
<span style="color: #666666; font-style: italic;"># BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER</span>
<span style="color: #000000; font-weight: bold;">function</span> function_B <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Function B.
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> function_A <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> function_D <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Function D.
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> function_C <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #666666; font-style: italic;"># FUNCTION CALLS</span>
<span style="color: #666666; font-style: italic;"># Pass parameter to function A</span>
function_A <span style="color: #ff0000;">&quot;Function A.&quot;</span>
function_B
<span style="color: #666666; font-style: italic;"># Pass parameter to function C</span>
function_C <span style="color: #ff0000;">&quot;Function C.&quot;</span>
function_D</pre></td></tr></table></div>

<p><img title="Bash &#13;&#10;Functions" border="0" alt="Bash Functions" src="http://zhuwenhao.com/wp-content/uploads/HLIC/1715a494be3c75154d84be2d17856678.gif" width="484" height="82" /></p>
<h3>14. Bash Select</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code79'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33979"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p339code79"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">PS3</span>=<span style="color: #ff0000;">'Choose one word: '</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># bash select</span>
<span style="color: #000000; font-weight: bold;">select</span> word <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;linux&quot;</span> <span style="color: #ff0000;">&quot;bash&quot;</span> <span style="color: #ff0000;">&quot;scripting&quot;</span> <span style="color: #ff0000;">&quot;tutorial&quot;</span> 
<span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;The word you have selected is: <span style="color: #007800;">$word</span>&quot;</span>
<span style="color: #666666; font-style: italic;"># Break, otherwise endless loop</span>
  <span style="color: #7a0874; font-weight: bold;">break</span>  
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></td></tr></table></div>

<p><img title="Bash Select" border="0" alt="Bash Select" src="http://zhuwenhao.com/wp-content/uploads/HLIC/bcb1e849a412444f2fbcf3a7308cfcf2.gif" width="214" height="108" /></p>
<h3>15. Case statement conditional</h3>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code80'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33980"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p339code80"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;What is your preferred programming / scripting language&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;1) bash&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;2) perl&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;3) phyton&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;4) c++&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;5) I do not know !&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #000000; font-weight: bold;">case</span>;
<span style="color: #666666; font-style: italic;">#simple case bash structure</span>
<span style="color: #666666; font-style: italic;"># note in this case $case is variable and does not have to</span>
<span style="color: #666666; font-style: italic;"># be named case this is just an example</span>
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$case</span> <span style="color: #000000; font-weight: bold;">in</span>
    <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You selected bash&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You selected perl&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You selected phyton&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000;">4</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You selected c++&quot;</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p><img title="bash case statement conditiona" border="0" alt="bash case statement &#13;&#10;conditiona" src="http://zhuwenhao.com/wp-content/uploads/HLIC/4786877f23908eb04883776adde01218.gif" width="340" height="134" /></p>
<h3>16. Bash quotes and quotations</h3>
<p>Quotations and quotes are important part of bash and bash scripting. Here are some bash quotes and quotations basics. </p>
<h4>16.1. Escaping Meta characters</h4>
<p>Before we start with quotes and quotations we should know something about escaping meta characters. Escaping will suppress a special meaning of meta characters and therefore meta characters will be read by bash literally. To do this we need to use backslash &quot;\&quot; character. Example: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code81'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33981"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p339code81"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Declare bash string variable</span>
<span style="color: #007800;">BASH_VAR</span>=<span style="color: #ff0000;">&quot;Bash Script&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># echo variable BASH_VAR</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$BASH_VAR</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#when meta character such us &quot;$&quot; is escaped with &quot;\&quot; it will be read literally</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> \<span style="color: #007800;">$BASH_VAR</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># backslash has also special meaning and it can be suppressed with yet another &quot;\&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;\<span style="color: #000099; font-weight: bold;">\&quot;</span></span></pre></td></tr></table></div>

<p><img title="escaping meta characters in bash" border="0" alt="escaping meta characters in &#13;&#10;bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/b0de260409d3d1029209f36eea711844.gif" width="202" height="69" /></p>
<h4>16.2. Single quotes</h4>
<p>Single quotes in bash will suppress special meaning of every meta characters. Therefore meta characters will be read literally. It is not possible to use another single quote within two single quotes not even if the single quote is escaped by backslash. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code82'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33982"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p339code82"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">#Declare bash string variable</span>
 <span style="color: #007800;">BASH_VAR</span>=<span style="color: #ff0000;">&quot;Bash Script&quot;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;"># echo variable BASH_VAR</span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$BASH_VAR</span>
&nbsp;
 <span style="color: #666666; font-style: italic;"># meta characters special meaning in bash is suppressed when  using single quotes </span>
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'$BASH_VAR  &quot;$BASH_VAR&quot;'</span></pre></td></tr></table></div>

<p><img title="Using single quotes in bash" border="0" alt="Using single quotes in &#13;&#10;bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/7db79df46a83eb27d201dec0eaebb011.gif" width="214" height="56" /></p>
<h4>16.3. Double Quotes</h4>
<p>Double quotes in bash will suppress special meaning of every meta characters except &quot;$&quot;, &quot;\&quot; and &quot;`&quot;. Any other meta characters will be read literally. It is also possible to use single quote within double quotes. If we need to use double quotes within double quotes bash can read them literally when escaping them with &quot;\&quot;. Example: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code83'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33983"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p339code83"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Declare bash string variable</span>
<span style="color: #007800;">BASH_VAR</span>=<span style="color: #ff0000;">&quot;Bash Script&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># echo variable BASH_VAR</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$BASH_VAR</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># meta characters and its special meaning in bash is </span>
<span style="color: #666666; font-style: italic;"># suppressed when using double quotes except &quot;$&quot;, &quot;\&quot; and &quot;`&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;It's <span style="color: #007800;">$BASH_VAR</span>  and <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$BASH_VAR</span><span style="color: #000099; font-weight: bold;">\&quot;</span> using backticks: <span style="color: #780078;">`date`</span>&quot;</span></pre></td></tr></table></div>

<p><img title="Using double quotes in bash" border="0" alt="Using double quotes in &#13;&#10;bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/513679ef8fd8955eb85d12e3ceb9eabd.gif" width="490" height="56" /></p>
<h4>16.4. Bash quoting with ANSI-C style</h4>
<p>There is also another type of quoting and that is ANSI-C. In this type of quoting characters escaped with &quot;\&quot; will gain special meaning according to the ANSI-C standard. </p>
<table border="1" cellspacing="0" cellpadding="2" width="702">
<tbody>
<tr>
<td valign="top" width="31">/a</td>
<td valign="top" width="326">alert (bell)</td>
<td valign="top" width="16">/b</td>
<td valign="top" width="327">backspace</td>
</tr>
<tr>
<td valign="top" width="31">/e</td>
<td valign="top" width="326">an escape character</td>
<td valign="top" width="16">/f</td>
<td valign="top" width="327">form feed</td>
</tr>
<tr>
<td valign="top" width="31">/n</td>
<td valign="top" width="326">newline</td>
<td valign="top" width="16">/r</td>
<td valign="top" width="327">carriage return</td>
</tr>
<tr>
<td valign="top" width="31">/t</td>
<td valign="top" width="326">horizontal tab</td>
<td valign="top" width="16">/v</td>
<td valign="top" width="327">vertical tab</td>
</tr>
<tr>
<td valign="top" width="31">\\</td>
<td valign="top" width="326">backslash</td>
<td valign="top" width="16">\`</td>
<td valign="top" width="327">single quote</td>
</tr>
<tr>
<td valign="top" width="31">\nnn</td>
<td valign="top" width="326">octal value of characters ( see [http://www.asciitable.com/ ASCII table] )</td>
<td valign="top" width="16">\xnn</td>
<td valign="top" width="327">hexadecimal value of characters ( see [http://www.asciitable.com/ ASCII table] )</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>The syntax fo ansi-c bash quoting is: $” . Here is an example: </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code84'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33984"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p339code84"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># as a example we have used \n as a new line, \x40 is hex value for @</span>
<span style="color: #666666; font-style: italic;"># and \56 is octal value for .</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> $<span style="color: #ff0000;">'web: www.linuxconfig.org\nemail: web\x40linuxconfig\56org'</span></pre></td></tr></table></div>

<p><img title="quoting in bash with ansi-c stype" border="0" alt="quoting in bash with ansi-c &#13;&#10;stype" src="http://zhuwenhao.com/wp-content/uploads/HLIC/6eb9043058d8b18cbd9c1824179b4f21.gif" width="202" height="56" /></p>
<h3>17. Arithmetic Operations</h3>
<h4>17.1. Bash Addition Calculator Example</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code85'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33985"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p339code85"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">RESULT1</span>=<span style="color: #007800;">$1</span>+<span style="color: #007800;">$2</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>+<span style="color: #007800;">$2</span>=<span style="color: #007800;">$RESULT1</span> <span style="color: #ff0000;">' -&gt; # let RESULT1=$1+$2'</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-i</span> RESULT2
<span style="color: #007800;">RESULT2</span>=<span style="color: #007800;">$1</span>+<span style="color: #007800;">$2</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>+<span style="color: #007800;">$2</span>=<span style="color: #007800;">$RESULT2</span> <span style="color: #ff0000;">' -&gt; # declare -i RESULT2; RESULT2=$1+$2'</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>+<span style="color: #007800;">$2</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$1</span> + <span style="color: #007800;">$2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #ff0000;">' -&gt; # $(($1 + $2))'</span></pre></td></tr></table></div>

<p><img title="Bash Addition Calculator" border="0" alt="Bash Addition Calculator" src="http://zhuwenhao.com/wp-content/uploads/HLIC/407b4a7ba59f8f7133611479c94e95c1.gif" width="304" height="69" /></p>
<h4>17.2. Bash Arithmetics</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code86'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33986"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code" id="p339code86"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'### let ###'</span>
<span style="color: #666666; font-style: italic;"># bash addition</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">ADDITION</span>=<span style="color: #000000;">3</span>+<span style="color: #000000;">5</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;3 + 5 =&quot;</span> <span style="color: #007800;">$ADDITION</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash subtraction</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">SUBTRACTION</span>=<span style="color: #000000;">7</span>-<span style="color: #000000;">8</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;7 - 8 =&quot;</span> <span style="color: #007800;">$SUBTRACTION</span> 
&nbsp;
<span style="color: #666666; font-style: italic;"># bash multiplication</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">MULTIPLICATION</span>=<span style="color: #000000;">5</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">8</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;5 * 8 =&quot;</span> <span style="color: #007800;">$MULTIPLICATION</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash division</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">DIVISION</span>=<span style="color: #000000;">4</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;4 / 2 =&quot;</span> <span style="color: #007800;">$DIVISION</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash modulus</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">MODULUS</span>=<span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">%</span>4
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;9 % 4 =&quot;</span> <span style="color: #007800;">$MODULUS</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash power of two</span>
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">POWEROFTWO</span>=<span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">**</span><span style="color: #000000;">2</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;2 ^ 2 =&quot;</span> <span style="color: #007800;">$POWEROFTWO</span>
&nbsp;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'### Bash Arithmetic Expansion ###'</span>
<span style="color: #666666; font-style: italic;"># There are two formats for arithmetic expansion: $[ expression ] </span>
<span style="color: #666666; font-style: italic;"># and $(( expression #)) its your choice which you use</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">4</span> + <span style="color: #000000;">5</span> = $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">4</span> + <span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">7</span> - <span style="color: #000000;">7</span> = $<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000;">7</span> - <span style="color: #000000;">7</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">4</span> x <span style="color: #000000;">6</span> = $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">3</span> = $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">6</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">3</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #000000;">7</span> = $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">8</span> <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #000000;">7</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">2</span> ^ <span style="color: #000000;">8</span> = $<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">**</span> <span style="color: #000000;">8</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'### Declare ###'</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;Please enter two numbers \c&quot;</span>
<span style="color: #666666; font-style: italic;"># read user input</span>
<span style="color: #c20cb9; font-weight: bold;">read</span> num1 num2
<span style="color: #7a0874; font-weight: bold;">declare</span> <span style="color: #660033;">-i</span> result
<span style="color: #007800;">result</span>=<span style="color: #007800;">$num1</span>+<span style="color: #007800;">$num2</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Result is:<span style="color: #007800;">$result</span> &quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash convert binary number 10001</span>
<span style="color: #007800;">result</span>=<span style="color: #000000;">2</span><span style="color: #666666; font-style: italic;">#10001</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$result</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash convert octal number 16</span>
<span style="color: #007800;">result</span>=<span style="color: #000000;">8</span><span style="color: #666666; font-style: italic;">#16</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$result</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># bash convert hex number 0xE6A</span>
<span style="color: #007800;">result</span>=<span style="color: #000000;">16</span><span style="color: #666666; font-style: italic;">#E6A</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$result</span></pre></td></tr></table></div>

<p><img title="Bash Arithmetic Operations" border="0" alt="Bash Arithmetic &#13;&#10;Operations" src="http://zhuwenhao.com/wp-content/uploads/HLIC/fe12e99a51abda736117c1529e27e4fe.gif" width="262" height="290" /></p>
<h4>17.3. Round floating point number</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code87'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33987"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p339code87"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># get floating point number</span>
<span style="color: #007800;">floating_point_number</span>=<span style="color: #000000;">3.3446</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$floating_point_number</span>
<span style="color: #666666; font-style: italic;"># round floating point number with bash</span>
<span style="color: #000000; font-weight: bold;">for</span> bash_rounded_number <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #000000; font-weight: bold;">%</span>.0f <span style="color: #007800;">$floating_point_number</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Rounded number with bash:&quot;</span> <span style="color: #007800;">$bash_rounded_number</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><img title="Round floating point number with&#13;&#10; bash" border="0" alt="Round &#13;&#10;floating point number with bash" src="http://zhuwenhao.com/wp-content/uploads/HLIC/637106928c4b70f81dc3d172293e4982.gif" width="178" height="56" /></p>
<h4>17.4. Bash floating point calculations</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code88'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33988"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p339code88"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># Simple linux bash calculator </span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Enter input:&quot;</span> 
<span style="color: #c20cb9; font-weight: bold;">read</span> userinput
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Result with 2 digits after decimal point:&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;scale=2; <span style="color: #007800;">${userinput}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span> 
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Result with 10 digits after decimal point:&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;scale=10; <span style="color: #007800;">${userinput}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span> 
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Result as rounded integer:&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$userinput</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bc</span></pre></td></tr></table></div>

<p><img title="Bash floating point calculations" border="0" alt="Bash floating point &#13;&#10;calculations" src="http://zhuwenhao.com/wp-content/uploads/HLIC/70aba42cc7621c34256b85420890de61.gif" width="256" height="134" /></p>
<h3>18. Redirections</h3>
<h4>18.1. STDOUT from bash script to STDERR</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code89'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33989"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p339code89"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Redirect this STDOUT to STDERR&quot;</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span></pre></td></tr></table></div>

<p>To proof that STDOUT is redirected to STDERR we can redirect script&#8217;s output to file:<br />
  <br /><img title="STDOUT from bash script to STDERR" border="0" alt="STDOUT from bash script to &#13;&#10;STDERR" src="http://zhuwenhao.com/wp-content/uploads/HLIC/d690ff886e17e0d3369cd0a9c90d0be5.gif" width="286" height="147" /></p>
<h4>18.2. STDERR from bash script to STDOUT</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code90'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33990"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p339code90"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
 <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$1</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span></pre></td></tr></table></div>

<p>To proof that STDERR is redirected to STDOUT we can redirect script&#8217;s output to file:<br />
  <br /><img title="STDERR from bash script to STDOUT" border="0" alt="STDERR from bash script to &#13;&#10;STDOUT" src="http://zhuwenhao.com/wp-content/uploads/HLIC/5098790c995e2ba672c395b11d75e797.gif" width="358" height="147" /></p>
<h4>18.3. stdout to screen</h4>
<p>The simple way to redirect a standard output ( stdout ) is to simply use any command, because by default stdout is automatically redirected to screen. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code91'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33991"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code91"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>partitions</pre></td></tr></table></div>

<p><img title="bash stdout to screen" border="0" alt="bash stdout to screen" src="http://zhuwenhao.com/wp-content/uploads/HLIC/36b19b9c3b3aa10b64fcd524ec221c4e.gif" width="484" height="121" /></p>
<h4>18.4. stdout to file</h4>
<p>Here we use &quot;&gt;&quot; to redirect stdout to a file &quot;partitions.txt&quot;. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code92'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33992"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code92"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>partitions <span style="color: #000000; font-weight: bold;">&gt;</span> partitions.txt</pre></td></tr></table></div>

<p><img title="bash stdout to file" border="0" alt="bash stdout to file" src="http://zhuwenhao.com/wp-content/uploads/HLIC/7f7ee48c4dbe394bbdf888f5a46536f2.gif" width="484" height="134" /></p>
<h4>18.5. stderr to file</h4>
<p>In this example you will redirect the standard error ( stderr ) to a file and stdout to a default screen. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code93'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33993"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code93"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-r</span> hda6 <span style="color: #000000; font-weight: bold;">*</span> . <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span> stderr.txt</pre></td></tr></table></div>

<p><img title="bash stderr to file" border="0" alt="bash stderr to file" src="http://zhuwenhao.com/wp-content/uploads/HLIC/b21c2271625b1e973e7468ba866faf32.gif" width="484" height="251" /></p>
<h4>18.6. stdout to stderr</h4>
<p>In this case the output of a command will be written to the same descriptor as a stderr. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code94'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33994"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code94"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-r</span> hda6 <span style="color: #000000; font-weight: bold;">*</span> . <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span> stderr.txt</pre></td></tr></table></div>

<p><img title="bash stdout to stderr" border="0" alt="bash stdout to stderr" src="http://zhuwenhao.com/wp-content/uploads/HLIC/700b9fe7e49c7cb9eb8731d1cf337dac.gif" width="484" height="225" /></p>
<h4>18.7. stderr to stdout</h4>
<p>In this case the stderr of a command will be written to the same descriptor as a stdout. </p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code95'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33995"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code95"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-r</span> hda6 <span style="color: #000000; font-weight: bold;">*</span> . <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> stderr.txt</pre></td></tr></table></div>

<p><img title="bash stderr to stdout" border="0" alt="bash stderr to stdout" src="http://zhuwenhao.com/wp-content/uploads/HLIC/ce9781383f648771ea2a4b5507bc4489.gif" width="484" height="225" /></p>
<h4>18.8. stderr and stdout to file</h4>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p339code96'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p33996"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p339code96"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-r</span> hda6 <span style="color: #000000; font-weight: bold;">*</span> . <span style="color: #000000; font-weight: bold;">&amp;&gt;</span> stderr_and_stdout.txt</pre></td></tr></table></div>

<p><img title="stderr and stdout to file" border="0" alt="stderr and stdout &#13;&#10;to file" src="http://zhuwenhao.com/wp-content/uploads/HLIC/dda4277bc3d3dc1d7c69b7f869eb14b8.gif" width="484" height="251" /></p>
]]></content:encoded>
			<wfw:commentRss>http://zhuwenhao.com/339/%e6%8a%80%e6%9c%af/%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1%e8%af%ad%e8%a8%80/bash/bash-scripting-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

