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

<channel>
	<title>Zulius &#187; bash</title>
	<atom:link href="http://www.zulius.com/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zulius.com</link>
	<description>Advanced Application Development</description>
	<lastBuildDate>Mon, 30 Aug 2010 20:44:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>bash-beauty: formatted output for bash scripts</title>
		<link>http://www.zulius.com/freebies/bash-beauty-output-for-bash-scripts/</link>
		<comments>http://www.zulius.com/freebies/bash-beauty-output-for-bash-scripts/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 23:46:33 +0000</pubDate>
		<dc:creator>Tim White</dc:creator>
				<category><![CDATA[freebies]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.zulius.com/?p=408</guid>
		<description><![CDATA[A bash function library for displaying beautiful script progress output.]]></description>
			<content:encoded><![CDATA[<div style="margin: 0 auto; text-align: center"><a title="Download" href="/scripts/bash-beauty.sh" target="_blank"><img src="/img/button-dl-orange.png" alt="" width="202" height="62" /></a></div>
<div style="float: right; padding: 0 5px; margin: 25px 0 0 0;width: 160px">
<div style="clear: both">
<div class="wp-caption alignright" style="width: 160px"><a id="ss1" href="/img/blog/bash-beauty/bash-beauty-ss1.jpg" rel="colorbox"><img title="bash-beauty screenshot #1" src="/img/blog/bash-beauty/bash-beauty-ss1-thumb.jpg" alt="bash-beauty screenshot #1" width="150" height="110" /></a><p class="wp-caption-text">screenshot #1</p></div><br />
<script type="text/javascript">jQuery(document).ready(function(){jQuery("#ss1").colorbox({title: 'basic usage'});});</script>
</div>
<div style="clear: both">
<div class="wp-caption alignright" style="width: 160px"><a id="ss2" href="/img/blog/bash-beauty/bash-beauty-ss2.jpg" rel="colorbox"><img title="bash-beauty screenshot #2" src="/img/blog/bash-beauty/bash-beauty-ss2-thumb.jpg" alt="bash-beauty screenshot #2" width="150" height="110" /></a><p class="wp-caption-text">screenshot #2</p></div><br />
<script type="text/javascript">jQuery(document).ready(function(){jQuery("#ss2").colorbox({title: 'Print multi-colored result statuses'});});</script><br />

</div>
<div style="clear: both">
<div class="wp-caption alignright" style="width: 160px"><a id="ss3" href="/img/blog/bash-beauty/bash-beauty-ss3.jpg" rel="colorbox"><img title="bash-beauty screenshot #3" src="/img/blog/bash-beauty/bash-beauty-ss3-thumb.jpg" alt="bash-beauty screenshot #3" width="150" height="110" /></a><p class="wp-caption-text">screenshot #3</p></div><br />
<script type="text/javascript">jQuery(document).ready(function(){jQuery("#ss3").colorbox({title: 'Prefix each task with a 14 digit timestamp'});});</script>
</div>
</div>
<h2>What is it?</h2>
<p>
Bash function library for displaying script progress output.  The functions imitate the display format of Linux boot messages.</p>
<h2>What does it do?</h2>
<ul>
<li>Logs/echo's script tasks and results in a columnar format</li>
<li>Appends each task line with a colored status result (terminal stdout only)</li>
<li>Makes your script's output look good and readable</li>
</ul>
<h2>Other Features</h2>
<ul>
<li>Customizable message column width</li>
<li>Quiet mode writes only to a specified log file</li>
<li>Optionally prefixes all messages with a 14 digit timestamp</li>
<li>Free</li>
</ul>
<h2>Requirements</h2>
<p style="padding-left: 30px;">
<a title="Perl" href="http://www.gnu.org/software/bash/" target="_blank">GNU Bourne-Again Shell (BASH)</a>
</p>
<h2>Functions</h2>
<p>The bash-beauty.sh library has 5 printing functions: </p>
<ul>
<li>
<h4 style="margin-top: 0">printTask</h4>
<pre>Prints a single task message padded with whitespace to default 80 characters.</pre>
<div>
<h4 class="usage" style="margin-top: 10px">Usage</h4>
<pre style="margin-bottom: 15px">
printTask [OPTION]... MESSAGE
</pre>
<h4 class="usage" style="margin-top: 10px">Options</h4>
<pre style="margin-bottom: 15px">
-t     flag to automatically prepend message with 14 digit timestamp
-l     specify log file to write to
-q     quiet mode. Do not output to stdout, only write to log file if supplied
-w     width of padded message column. Defaults to 80 characters.
</pre>
</div>
</li>
<li>
<h4 style="margin-top: 0">printOk</h4>
<pre>Prints a green OK status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: green">[  OK  ]</span></pre>
<div>
<h4 class="usage" style="margin-top: 10px">Usage</h4>
<pre style="margin-bottom: 15px">
printOk [OPTION]...
</pre>
<h4 class="usage" style="margin-top: 10px">Options</h4>
<pre style="margin-bottom: 15px">
-l     specify log file to write to
-q     quiet mode. Do not output to stdout, only write to log file if supplied
</pre>
</div>
</li>
<li>
<h4 style="margin-top: 0">printFail</h4>
<pre>Prints a red failure status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: red">[FAILED]</span></pre>
<div>
<h4 class="usage" style="margin-top: 10px">Usage</h4>
<pre style="margin-bottom: 15px">
printFail [OPTION]... [MESSAGE]
</pre>
<h4 class="usage" style="margin-top: 10px">Options</h4>
<pre style="margin-bottom: 15px">
-l     specify log file to write to
-q     quiet mode. Do not output to stdout, only write to log file if supplied
</pre>
</div>
</li>
<li>
<h4 style="margin-top: 0">printWarn</h4>
<pre>Prints a yellow warning status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: yellow">[ WARN ]</span></pre>
<div>
<h4 class="usage" style="margin-top: 10px">Usage</h4>
<pre style="margin-bottom: 15px">
 printWarn [OPTION]...
</pre>
<h4 class="usage" style="margin-top: 10px">Options</h4>
<pre style="margin-bottom: 15px">
-l     specify log file to write to
-q     quiet mode. Do not output to stdout, only write to log file if supplied
</pre>
</div>
</li>
<li>
<h4 style="margin-top: 0">printInfo</h4>
<pre>Prints a blue info status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: #2E97D7">[ INFO ]</span></pre>
<div>
<h4 class="usage" style="margin-top: 10px">Usage</h4>
<pre style="margin-bottom: 15px">
 printInfo [OPTION]...
</pre>
<h4 class="usage" style="margin-top: 10px">Options</h4>
<pre style="margin-bottom: 15px">
-l     specify log file to write to
-q     quiet mode. Do not output to stdout, only write to log file if supplied
</pre>
</div>
</li>
</ul>
<h2>Example 1</h2>
<p style="margin-top: 10px">Source the bash-beauty.sh file</p>
<pre class="brush: bash; first-line: 1; gutter: true;">. /path/to/bash-beauty.sh</pre>
<p>Execute the printTask function with a description of your task, execute your code, and then execute an appropriate status function: <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace;">printOk(), printFail(), printWarn(), printInfo()</span></p>
<pre class="brush: bash; first-line: 2; gutter: true;">printTask &quot;Executing arbitrary code&quot;
# your code here
printOk
</pre>
<p>Output</p>
<div class="wp-caption" style="float: left; margin: -15px 0 15px 0">
<img title="bash-beauty #1 example output" src="/img/blog/bash-beauty/bash-beauty-ss4.jpg" alt="bash-beauty #1 example output"/>
</div>
<div style="clear: both"></div>
<h2>Example 2</h2>
<p>If your tasks are commands that print output to stdout/stderr, then you'll probably want to capture their output to keep things looking nice. You may also want to examine each command's result code to determine the appropriate status to print.</p>
<p>This example script attempts to touch file /foo/bar.  If successful, it prints a green status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: green">[  OK  ]</span>.  If unsuccessful, it prints a red failure status result <span style="font-family:  'Lucida Console', 'Courier New', Courier, monospace; color: red">[FAILED]</span>, the error encountered, and exits.</p>
<pre class="brush: bash; first-line: 1; gutter: true;">
#!/bin/bash

# step 1 - source the function library
. ./lib/bash-beauty.sh

# step 2 - use printTask to display the task message
printTask &quot;Attempting to touch /foo/bar&quot;

# step 3 - execute the command,
# redirect command stderr to stdout,
# capture output to variable OUTPUT
OUTPUT=$(touch /foo/bar 2&gt;&amp;1)

# step 4 - examine the command's result code
if [ ! &quot;$?&quot; == 0 ]; then
  printFail &quot;$OUTPUT&quot;
  exit 1
fi
printOk
</pre>
<p>Output</p>
<div class="wp-caption" style="float: left; margin: -15px 0 15px 0">
<img title="bash-beauty example #2 output" src="/img/blog/bash-beauty/bash-beauty-ss5.jpg" alt="bash-beauty example #2 output"/>
</div>
<div style="clear: both"></div>
<h2>Example 3</h2>
<p style="margin-top: 10px">This example script prints to both the terminal and a log file, prefixes each task with a timestamp, and has a custom task message column width.</p>
<pre class="brush: bash; first-line: 1; gutter: true;">
#!/bin/bash

. ./lib/bash-beauty.sh

# set up log file path variable
LOG_FILE=&quot;/tmp/foobar.log&quot;

printTask -t -w 50 -l &quot;$LOG_FILE&quot; &quot;Process running&quot;
sleep 1s    # your code here
printOk -l &quot;$LOG_FILE&quot;

printTask -t -w 50 -l &quot;$LOG_FILE&quot; &quot;Implementing and executing&quot;
sleep 1s
printOk -l &quot;$LOG_FILE&quot;

printTask -t -w 50 -l &quot;$LOG_FILE&quot; &quot;Doing stuff&quot;
sleep 1s
printOk -l &quot;$LOG_FILE&quot;

printTask -t -w 50 -l &quot;$LOG_FILE&quot; &quot;Gittin 'er done&quot;
sleep 1s
printFail -l &quot;$LOG_FILE&quot; &quot;Max hillbilly tolerance level reached&quot;
</pre>
<p>Output to terminal</p>
<div class="wp-caption" style="float: left; margin: -15px 0 15px 0">
<img title="bash-beauty example #2 output" src="/img/blog/bash-beauty/bash-beauty-ss6.jpg" alt="bash-beauty example #3 output"/>
</div>
<div style="clear: both"></div>
<p>Contents of log file at /tmp/foobar.log</p>
<pre class="brush: plain;">
20091114161952  Process running                   [  OK  ]
20091114161953  Implementing and executing        [  OK  ]
20091114161954  Doing stuff                       [  OK  ]
20091114161955  Gittin 'er done                   [FAILED]

Max hillbilly tolerance level reached
</pre>
<h2>Change Log</h2>
<p><strong>0.0.1 - 20091113</strong></p>
<ul>
<li>initial release</li>
</ul>
<div style="margin: 0 auto; text-align: center"><a title="Download" href="/scripts/bash-beauty.sh" target="_blank"><img src="/img/button-dl-orange.png" alt="" width="202" height="62" /></a></div>
<img src="http://www.zulius.com/blog/?ak_action=api_record_view&id=408&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.zulius.com/freebies/bash-beauty-output-for-bash-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
