<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Food for Thought</title>
	<atom:link href="http://jeredsutton.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeredsutton.com</link>
	<description>Musings and snippets that I have found helpful, interesting, motivational, etc.</description>
	<lastBuildDate>Tue, 07 May 2013 08:11:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jeredsutton.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/02ec80ac6cad05e8962e792f15392a33?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Food for Thought</title>
		<link>http://jeredsutton.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jeredsutton.com/osd.xml" title="Food for Thought" />
	<atom:link rel='hub' href='http://jeredsutton.com/?pushpress=hub'/>
		<item>
		<title>Bash Nagios plugin</title>
		<link>http://jeredsutton.com/2013/04/28/bash-nagios-plugin/</link>
		<comments>http://jeredsutton.com/2013/04/28/bash-nagios-plugin/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 04:43:39 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[IFTTT]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=208</guid>
		<description><![CDATA[Today lets have a look at one way to construct a nagios plugin in bash. I would usually write these in perl, but sometimes that is not possible. This plugin is actually written to be executed using NRPE. #!/bin/bash # &#8230; <a href="http://jeredsutton.com/2013/04/28/bash-nagios-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=208&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today lets have a look at one way to construct a nagios plugin in bash. I would usually write these in perl, but sometimes that is not possible. This plugin is actually written to be executed using NRPE.</p>
<pre class="prettyprint"><code>#!/bin/bash
# bash nagios plugin

###
# Variables
###
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=-1
TO_RETURN=${OK}
TO_OUTPUT=''

# Print usage information and exit
print_usage(){
    echo -e "\n" \
    "usage: ./check_uptime -w 20 -c 30 \n" \
    "\n" \
    "-w &lt;days&gt;    warning value\n" \
    "-c &lt;days&gt;    critical value\n" \
    "-h           this help\n" \
    "\n" &amp;&amp; exit 1
}

###
# Options
###

# Loop through $@ to find flags
while getopts ":hw:c:" FLAG; do
    case "${FLAG}" in
        w) # Warning value
            WARNING_VALUE="${OPTARG}" ;;
        c) # Critical value
            CRITICAL_VALUE="${OPTARG}" ;;
        h) # Print usage information
            HELP=1;;
        [:?]) # Print usage information
            print_usage;;
    esac
done

###
# Functions
###

log_date(){
    echo $(date +"%b %e %T")
}

error() {
    NOW=$(log_date)
    echo "${NOW}: ERROR: $1"
    exit 1
}

warning() {
    NOW=$(log_date)
    echo "${NOW}: WARNING: $1"
}

info() {
    NOW=$(log_date)
    echo "${NOW}: INFO: $1"
}

# Do something
get_cmd_output(){
    #generate output
    echo `uptime | sed 's/.*up \([0-9]*\) day.*/\1/'` || error "failed to run command"
}

###
# Program execution
###
[ "${HELP}" ] &amp;&amp; print_usage

if [ ${WARNING_VALUE} ] &amp;&amp; [ ${CRITICAL_VALUE} ]
then
    CMD_OUTPUT=$(get_cmd_output)
else
    print_usage
fi

if [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt ${CRITICAL_VALUE} ]
then
    TO_RETURN=${CRITICAL}
elif [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt ${WARNING_VALUE} ]
then
    TO_RETURN=${WARNING}
elif [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt 0 ]
then
    TO_RETURN=${OK}
else
    TO_RETURN=${UNKNOWN}
fi

if [ $TO_RETURN == ${CRITICAL} ]
then
    TO_OUTPUT="CRITICAL "
elif [ $TO_RETURN == ${WARNING} ]
then
    TO_OUTPUT="WARNING "
elif [ ${TO_RETURN} == ${OK} ]
then
    TO_OUTPUT="OK "
else
    TO_OUTPUT="UNKNOWN "
fi

TO_OUTPUT="${TO_OUTPUT}| uptime=${CMD_OUTPUT};$WARNING_VALUE;$CRITICAL_VALUE"

echo "$TO_OUTPUT";
exit $TO_RETURN;
</code></pre>
<p>Lets break it down…</p>
<pre class="prettyprint"><code>OK=0
WARNING=1
CRITICAL=2
UNKNOWN=-1
</code></pre>
<p>We define some readable names for the return codes.</p>
<pre class="prettyprint"><code>TO_RETURN=${OK}
</code></pre>
<p>Set the initial return value to OK.</p>
<pre class="prettyprint"><code># Do something
get_cmd_output(){
    #generate output
    echo `uptime | sed 's/.*up \([0-9]*\) day.*/\1/'` || error "failed to run command"
}
</code></pre>
<p>Function to obtain the value we want to check. In this case uptime.</p>
<pre class="prettyprint"><code>if [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt ${CRITICAL_VALUE} ]
then
    TO_RETURN=${CRITICAL}
elif [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt ${WARNING_VALUE} ]
then
    TO_RETURN=${WARNING}
elif [ "${CMD_OUTPUT}" ] &amp;&amp; [ ${CMD_OUTPUT} -gt 0 ]
then
    TO_RETURN=${OK}
else
    TO_RETURN=${UNKNOWN}
fi
</code></pre>
<p>Check the value of uptime against our warning and critical values.</p>
<pre class="prettyprint"><code>if [ $TO_RETURN == ${CRITICAL} ]
then
    TO_OUTPUT="CRITICAL "
elif [ $TO_RETURN == ${WARNING} ]
then
    TO_OUTPUT="WARNING "
elif [ ${TO_RETURN} == ${OK} ]
then
    TO_OUTPUT="OK "
else
    TO_OUTPUT="UNKNOWN "
fi
</code></pre>
<p>Set the visible output of the plugin. This output is not used by nagios.</p>
<pre class="prettyprint"><code>TO_OUTPUT="${TO_OUTPUT}| uptime=${CMD_OUTPUT};$WARNING_VALUE;$CRITICAL_VALUE"
</code></pre>
<p>Construct the output string according to the nagios plugin developer guidelines.</p>
<p>Stay tuned. The perl version will be out soon.</p>
<p>For more information see:<br />
<a href="http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201">http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN201</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/208/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=208&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/04/28/bash-nagios-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>ssh-copy-id missing in OS X</title>
		<link>http://jeredsutton.com/2013/04/21/ssh-copy-id-missing-in-os-x/</link>
		<comments>http://jeredsutton.com/2013/04/21/ssh-copy-id-missing-in-os-x/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 02:25:21 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[IFTTT]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=159</guid>
		<description><![CDATA[Not sure if anyone else has noticed, but OS X is missing ssh-copy-id. This utility is included with the ssh client in most major linux distros. As it turns out, it is just a shell script. #!/bin/sh # Shell script &#8230; <a href="http://jeredsutton.com/2013/04/21/ssh-copy-id-missing-in-os-x/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=159&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Not sure if anyone else has noticed, but OS X is missing ssh-copy-id. This utility is included with the ssh client in most major linux distros. As it turns out, it is just a shell script.</p>
<pre class="prettyprint"><code>#!/bin/sh

# Shell script to install your public key on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/id_rsa.pub"

if [ "-i" = "$1" ]; then
  shift
  # check if we have 2 parameters left, if so the first is the new ID file
  if [ -n "$2" ]; then
    if expr "$1" : ".*\.pub" &gt; /dev/null ; then
      ID_FILE="$1"
    else
      ID_FILE="$1.pub"
    fi
    shift         # and this should leave $1 as the target name
  fi
else
  if [ x$SSH_AUTH_SOCK != x ] &amp;&amp; ssh-add -L &gt;/dev/null 2&gt;&amp;1; then
    GET_ID="$GET_ID ssh-add -L"
  fi
fi

if [ -z "`eval $GET_ID`" ] &amp;&amp; [ -r "${ID_FILE}" ] ; then
  GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
  echo "$0: ERROR: No identities found" &gt;&amp;2
  exit 1
fi

if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  echo "Usage: $0 [-i [identity_file]] [user@]machine" &gt;&amp;2
  exit 1
fi

{ eval "$GET_ID" ; } | ssh ${1%:} "umask 077; test -d .ssh || mkdir .ssh ; cat &gt;&gt; .ssh/authorized_keys" || exit 1

cat &lt;&lt;EOF
Now try logging into the machine, with "ssh '${1%:}'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF
</code></pre>
<p>Go forth, copy, paste, chmod and happily deploy your ssh keys with ease.</p>
<p>P.S. For those who don&#8217;t know what I mean by chmod, see the following.</p>
<pre class="prettyprint"><code>chmod +x ./ssh-copy-id
</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/159/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=159&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/04/21/ssh-copy-id-missing-in-os-x/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Bash Parallel Execution</title>
		<link>http://jeredsutton.com/2013/04/14/bash-parallel-execution/</link>
		<comments>http://jeredsutton.com/2013/04/14/bash-parallel-execution/#comments</comments>
		<pubDate>Sun, 14 Apr 2013 16:25:30 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=151</guid>
		<description><![CDATA[If you have ever wanted an easy way to execute multiple jobs in parallel in bash, then this is the snippet for you. This was originally posted on Stack Exchange. It has been modified a bit. #!/bin/bash #how many jobs &#8230; <a href="http://jeredsutton.com/2013/04/14/bash-parallel-execution/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=151&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you have ever wanted an easy way to execute multiple jobs in parallel in bash, then this is the snippet for you. This was originally posted on <a href="http://stackoverflow.com/questions/1537956/bash-limit-the-number-of-concurrent-jobs/1685440#1685440">Stack Exchange</a>. It has been modified a bit.</p>
<pre class="prettyprint"><code>#!/bin/bash

#how many jobs to run at one time
JOBS_AT_ONCE=20

# The bgxupdate and bgxlimit functions below allow for
# running X jobs in parallel in bash.  They are taken from:
# http://stackoverflow.com/questions/1537956/bash-limit-the-number-of-concurrent-jobs/1685440#1685440

# bgxupdate - update active processes in a group.
#   Works by transferring each process to new group
#   if it is still active.
# in:  bgxgrp - current group of processes.
# out: bgxgrp - new group of processes.
# out: bgxcount - number of processes in new group.

bgxupdate() {
    bgxoldgrp=${bgxgrp}
    bgxgrp=""
    ((bgxcount = 0))
    bgxjobs=" $(jobs -pr | tr '\n' ' ')"
    for bgxpid in ${bgxoldgrp} ; do
        echo "${bgxjobs}" | grep " ${bgxpid} " &gt;/dev/null 2&gt;&amp;1
        if [[ $? -eq 0 ]] ; then
            bgxgrp="${bgxgrp} ${bgxpid}"
            ((bgxcount = bgxcount + 1))
        fi
    done
}

# bgxlimit - start a sub-process with a limit.

#   Loops, calling bgxupdate until there is a free
#   slot to run another sub-process. Then runs it
#   an updates the process group.
# in:  $1     - the limit on processes.
# in:  $2+    - the command to run for new process.
# in:  bgxgrp - the current group of processes.
# out: bgxgrp - new group of processes

bgxlimit() {
    bgxmax=$1 ; shift
    bgxupdate
    while [[ ${bgxcount} -ge ${bgxmax} ]] ; do
        sleep 1
        bgxupdate
    done
    if [[ "$1" != "-" ]] ; then
        $* &amp;
        bgxgrp="${bgxgrp} $!"
    fi
}

bgxgrp="process_group_1"
for LINE in `cat hosts`
do
    CHECK_SCRIPT='echo $(hostname),$(cat /etc/debian_version)'
    bgxlimit $JOBS_AT_ONCE ssh ${LINE} "${CHECK_SCRIPT}"
done
# Wait until all queued processes are done.

bgxupdate
while [[ ${bgxcount} -ne 0 ]] ; do
    oldcount=${bgxcount}
    while [[ ${oldcount} -eq ${bgxcount} ]] ; do
        sleep 1
        bgxupdate
    done
done
</code></pre>
<p>In this script the primary changes are defining the max number of simultaneous jobs, as well as doing somewhat useful work in returning the hostname and the debian version.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/151/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=151&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/04/14/bash-parallel-execution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Turboprop</title>
		<link>http://jeredsutton.com/2013/04/07/turboprop/</link>
		<comments>http://jeredsutton.com/2013/04/07/turboprop/#comments</comments>
		<pubDate>Sun, 07 Apr 2013 16:25:30 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=152</guid>
		<description><![CDATA[As an extension of my previous post on parallel execution I present turboprop. The initial version of this script will perform an optimization of a mysql database with multiple tables running at the same time. In the future it may &#8230; <a href="http://jeredsutton.com/2013/04/07/turboprop/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=152&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>As an extension of my previous post on parallel execution I present turboprop. The initial version of this script will perform an optimization of a mysql database with multiple tables running at the same time. In the future it may be extended to allow for more operations from the command line such as mysql dumps.</p>
<pre class="prettyprint"><code>#!/bin/bash
# turboprop

# how many jobs to run at one time
JOBS_AT_ONCE=20
# Command to run in parallel in this case mysqlcheck -o
COMMAND="mysqlcheck -o"

# Print usage information and exit
print_usage(){
    echo -e "\n" \
    "usage: ./turboprop -d databasename \n" \
    "Optimizes mysql tables in parallel\n" \
    "-d &lt;databasename&gt;      Database to optimize\n" \
    "-h                     this help\n" \
    "\n" &amp;&amp; exit 1
}

###
# Options
###

# Loop through $@ to find flags
while getopts ":d:" FLAG; do
    case "${FLAG}" in
        d) # Database name
            DB=${OPTARG} ;;
        h) # Print usage
            print_usage;;
        [:?]) print_usage;;
    esac
done

[ ! ${DB} ] &amp;&amp; print_usage

###
# Functions
###

# The bgxupdate and bgxlimit functions below allow for
# running X jobs in parallel in bash.  They are taken from:
# http://stackoverflow.com/questions/1537956/bash-limit-the-number-of-concurrent-jobs/1685440#1685440

# bgxupdate - update active processes in a group.
#   Works by transferring each process to new group
#   if it is still active.
# in:  bgxgrp - current group of processes.
# out: bgxgrp - new group of processes.
# out: bgxcount - number of processes in new group.

bgxupdate() {
    bgxoldgrp=${bgxgrp}
    bgxgrp=""
    ((bgxcount = 0))
    bgxjobs=" $(jobs -pr | tr '\n' ' ')"
    for bgxpid in ${bgxoldgrp} ; do
        echo "${bgxjobs}" | grep " ${bgxpid} " &gt;/dev/null 2&gt;&amp;1
        if [[ $? -eq 0 ]] ; then
            bgxgrp="${bgxgrp} ${bgxpid}"
            ((bgxcount = bgxcount + 1))
        fi
    done
}

# bgxlimit - start a sub-process with a limit.

#   Loops, calling bgxupdate until there is a free
#   slot to run another sub-process. Then runs it
#   an updates the process group.
# in:  $1     - the limit on processes.
# in:  $2+    - the command to run for new process.
# in:  bgxgrp - the current group of processes.
# out: bgxgrp - new group of processes

bgxlimit() {
    bgxmax=$1 ; shift
    bgxupdate
    while [[ ${bgxcount} -ge ${bgxmax} ]] ; do
        sleep 1
        bgxupdate
    done
    if [[ "$1" != "-" ]] ; then
        $* &amp;
        bgxgrp="${bgxgrp} $!"
    fi
}

###
# Program Execution
###

bgxgrp="process_group_1"
for TABLE in `mysql ${DB} -e 'show tables'`
do
    bgxlimit ${JOBS_AT_ONCE} ${COMMAND} ${TABLE}
done

# Wait until all queued processes are done.

bgxupdate
while [[ ${bgxcount} -ne 0 ]] ; do
    oldcount=${bgxcount}
    while [[ ${oldcount} -eq ${bgxcount} ]] ; do
        sleep 1
        bgxupdate
    done
done
</code></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=152&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/04/07/turboprop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Agile vs Agile</title>
		<link>http://jeredsutton.com/2013/04/01/agile-vs-agile/</link>
		<comments>http://jeredsutton.com/2013/04/01/agile-vs-agile/#comments</comments>
		<pubDate>Mon, 01 Apr 2013 20:12:05 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/2013/04/01/agile-vs-agile/</guid>
		<description><![CDATA[I have had occasion recently to read about different development methodologies, and in doing so I realized that I had a fundamental misunderstanding of agile. What struck me as interesting is that so many people derive so many different meanings &#8230; <a href="http://jeredsutton.com/2013/04/01/agile-vs-agile/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=244&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I have had occasion recently to read about different development methodologies, and in doing so I realized that I had a fundamental misunderstanding of agile.</p>
<p>What struck me as interesting is that so many people derive so many different meanings from this word. Here are just a few:</p>
<p><span id="more-244"></span></p>
<h2>The Literate</h2>
<p>ag·ile<br />
/ˈajəl/</p>
<p><strong>Adjective</strong><br />
Able to move quickly and easily: &#8220;as agile as a monkey&#8221;; &#8220;an agile mind&#8221;.</p>
<h2>The Manager</h2>
<p>We can have more features faster if we are more agile.</p>
<h2>The Developer</h2>
<p>I can spend more time coding new features and less time reading specs and writing documentation.</p>
<p>Lets see what we get when we go to the source.</p>
<p><a title="http://agilemanifesto.org/">The Agile Manifesto</a></p>
<blockquote><p>We are uncovering better ways of developing software by doing it and helping others do it. Through this work we have come to value:</p>
<ul>
<li>Individuals and interactions over processes and tools</li>
<li>Working software over comprehensive documentation</li>
<li>Customer collaboration over contract negotiation</li>
<li>Responding to change over following a plan</li>
</ul>
<p>That is, while there is value in the items on<br />
the right, we value the items on the left more.</p></blockquote>
<p>Hmmm. Seems like we have some people who are not on the same page. It seems to me that agile in the context of software engineering is an ideology. If we look further it seems that this same group provided 12 principles that provide guidelines on defining and evaluating specific implementations.</p>
<p><a title="http://agilemanifesto.org/principles.html">Principles behind the Agile Manifesto</a></p>
<p>The manifesto and the principles provide some solid guidelines, but they are not a fully fleshed out software development method. Enter the framework.</p>
<p>I think that when most people think of agile, they are thinking of something like scrum. It is important to understand that scrum is to agile as a fiesta is to a ford car.</p>
<p>It is easy to dismiss agile processes entirely because scrum is not a good fit. If you carefully examine the ideology of agile, it is actually quite versatile. This is because it only defines relative values. For example, just because working software is of more value than comprehensive documentation, it does not mean that comprehensive documentation can not or should not exist.</p>
<p>By gaining a fuller understanding of the core ideas of agile development, we can take advantage of the benefits while avoiding the common misunderstandings.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/244/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/244/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=244&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/04/01/agile-vs-agile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Bash Best(ish) practices part 4</title>
		<link>http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/</link>
		<comments>http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/#comments</comments>
		<pubDate>Sat, 30 Mar 2013 02:33:28 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=150</guid>
		<description><![CDATA[This is the final post in the series. I will follow up this post by commiting my code to github for easy access. #!/bin/bash # bash template # Print usage information and exit print_usage(){ echo -e "\n" \ "usage: ./bashtemplate &#8230; <a href="http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=150&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is the final post in the series. I will follow up this post by commiting my code to github for easy access.</p>
<pre class="prettyprint"><code>#!/bin/bash
# bash template

# Print usage information and exit
print_usage(){
    echo -e "\n" \
    "usage: ./bashtemplate -o option \n" \
    "\n" \
    "-o &lt;option&gt;    an option\n" \
    "-h             this help\n" \
    "\n" &amp;&amp; exit 1
}

###
# Options
###

# Loop through $@ to find flags
while getopts ":ho:" FLAG; do
    case "${FLAG}" in
        o) # Our option
            OPTION="${OPTARG}" ;;
        h) # Print usage information
            HELP=1;;
        [:?]) # Print usage information
            print_usage;;
    esac
done

###
# Functions
###

log_date(){
    echo $(date +"%b %e %T")
}

error() {
    NOW=$(log_date)
    echo "${NOW}: ERROR: $1"
    exit 1
}

warning() {
    NOW=$(log_date)
    echo "${NOW}: WARNING: $1"
}

info() {
    NOW=$(log_date)
    echo "${NOW}: INFO: $1"
}

# Do something
do_something(){
    info "Doing something..."
    warning "Encountered an anomaly while doing something."
}

###
# Program execution
###

[ "${HELP}" ] &amp;&amp; print_usage

if [ ${OPTION} ]
then
    info "Executing do_something()"
    do_something
else
    print_usage
fi
</code></pre>
<p>I have removed much of the extraneous stuff from the previous examples as I would like this to be generally useful for writing scripts.</p>
<p>The final element to cover is debugging. In bash the following options are available:</p>
<p>-e Stops execution if any external program returns non-zero.</p>
<p>-x Trace what is being executed.</p>
<p>-n Don&#8217;t execute. Useful for syntax errors.</p>
<p>These can also be turned on and off in the script with &#8220;set&#8221;.</p>
<p>Lets see them in action.</p>
<p>-x</p>
<pre class="prettyprint"><code>$ bash -x ./bashtemplate
+ getopts :ho: FLAG
+ '[' '' ']'
+ '[' ']'
+ print_usage
+ echo -e '\n' 'usage: ./bashtemplate -o option \n' '\n' '-o &lt;option&gt;    an option\n' '-h             this help\n' '\n'

 usage: ./bashtemplate -o option

 -o &lt;option&gt;    an option
 -h             this help


+ exit 1
</code></pre>
<p>-n I introduced a syntax error.</p>
<pre class="prettyprint"><code>$ bash -n ./bashtemplate
./bashtemplate: line 70: syntax error near unexpected token `else'
./bashtemplate: line 70: `else'
</code></pre>
<p>-e I added a grep that would fail followed by an echo statement.</p>
<pre class="prettyprint"><code>$ bash -e ./bashtemplate
$
</code></pre>
<p>I hope this series has proven useful for someone other than myself, but if it has not I will never know.</p>
<p><a href="https://github.com/jsutton/bashtemplate">https://github.com/jsutton/bashtemplate</a></p>
<p><a href="http://jeredsutton.com/2013/03/24/bash-bestish-practices-part-3/" title="Bash Best(ish) practices part 3">Bash Best(ish) practices part 3</a><br />
<a href="http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/" title="Bash Best(ish) practices part 2">Bash Best(ish) practices part 2</a><br />
<a href="http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/" title="Bash Best(ish) practices part 1">Bash Best(ish) practices part 1</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/150/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/150/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=150&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Bash Best(ish) practices part 3</title>
		<link>http://jeredsutton.com/2013/03/24/bash-bestish-practices-part-3/</link>
		<comments>http://jeredsutton.com/2013/03/24/bash-bestish-practices-part-3/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 16:25:27 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=149</guid>
		<description><![CDATA[This is our third installment of the best(ish) series. In this installment we will look at some basic logging functions that simplify life. Here is the code so far. #!/bin/bash # Copy some files # Print usage information and exit &#8230; <a href="http://jeredsutton.com/2013/03/24/bash-bestish-practices-part-3/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=149&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is our third installment of the best(ish) series. In this installment we will look at some basic logging functions that simplify life. Here is the code so far.</p>
<pre class="prettyprint"><code>#!/bin/bash
# Copy some files

# Print usage information and exit
print_usage(){
    echo -e "\n" \
    "usage: ./copy_stuff -s sourcedir -d destdir \n" \
    "\n" \
    "-s &lt;sourcedir&gt; source directory\n" \
    "-d &lt;destdir&gt;   destination directory\n" \
    "-h             this help\n" \
    "\n" &amp;&amp; exit 1
}

###
# Options
###

# Loop through $@ to find flags
while getopts ":hs:d:" FLAG; do
    case "${FLAG}" in
        s) # Our source
            SOURCE="${OPTARG}" ;;
        d) # Our destination
            DEST="${OPTARG}" ;;
        h) # Print usage information
            HELP=1;;
        [:?]) # Print usage information
            print_usage;;
    esac
done

###
# Functions
###

log_date(){
    echo $(date +"%b %e %T")
}

error() {
    NOW=$(log_date)
    echo "${NOW}: ERROR: $1"
    exit 1
}

warning() {
    NOW=$(log_date)
    echo "${NOW}: WARNING: $1"
}

info() {
    NOW=$(log_date)
    echo "${NOW}: INFO: $1"
}

# Copy some files from one place to another
copy_files(){
    info "Copying ${SOURCE}/foo to ${DEST}/foo"
    info "Copying ${SOURCE}/bar to ${DEST}/bar"
    info "Copying ${SOURCE}/baz/foo to ${DEST}/baz/foo"
    info "Copying ${SOURCE}/baz/bar/foo to ${DEST}/baz/bar/foo"
    info "Copying ${SOURCE}/dir/somefile to ${DEST}/dir/someotherfile"
    echo
}

# Restart some service
restart_service(){
    info "Stopping the service"
    info "Making sure the service is stopped"
    info "Reticulating splines..."
    warning "splines not reticulated"
    info "Starting the service"
    echo
}

# Test to see if the service is running
test_service(){
    info "service is tested"
}

###
# Program execution
###

[ "${HELP}" ] &amp;&amp; print_usage

if [ ${SOURCE} ] &amp;&amp; [ ${DEST} ]
then
    info "Copying files from ${SOURCE} to ${DEST}"
    copy_files
    info "Restarting the service"
    restart_service
    info "Testing the service"
    test_service
fi
</code></pre>
<p>Lets break it down…</p>
<pre class="prettyprint"><code>log_date(){
    echo $(date +"%b %e %T")
}

error() {
    NOW=$(log_date)
    echo "${NOW}: ERROR: $1"
    exit 1
}

warning() {
    NOW=$(log_date)
    echo "${NOW}: WARNING: $1"
}

info() {
    NOW=$(log_date)
    echo "${NOW}: INFO: $1"
}
</code></pre>
<p>We have some new friends in the form of error,warning and info. These functions simply tag some provided text with an indicator of the type of output and also provide timestamp information. They save us just enough time to prove useful as we will see in a moment.</p>
<pre class="prettyprint"><code>    info "Reticulating splines..."
    warning "splines not reticulated"
</code></pre>
<p>You can see here that all we have to do is specify the logger we want to use and the particular output.</p>
<p>Let&#8217;s see it run.</p>
<pre class="prettyprint"><code># ./copy_stuff -s /dir1 -d /dir2
Mar 9 09:16:00: INFO: Copying files from /dir1 to /dir2
Mar 9 09:16:00: INFO: Copying /dir1/foo to /dir2/foo
Mar 9 09:16:01: INFO: Copying /dir1/bar to /dir2/bar
Mar 9 09:16:01: INFO: Copying /dir1/baz/foo to /dir2/baz/foo
Mar 9 09:16:01: INFO: Copying /dir1/baz/bar/foo to /dir2/baz/bar/foo
Mar 9 09:16:01: INFO: Copying /dir1/dir/somefile to /dir2/dir/someotherfile

Mar 9 09:16:01: INFO: Restarting the service
Mar 9 09:16:01: INFO: Stopping the service
Mar 9 09:16:01: INFO: Making sure the service is stopped
Mar 9 09:16:01: INFO: Reticulating splines...
Mar 9 09:16:01: WARNING: splines not reticulated
Mar 9 09:16:01: INFO: Starting the service

Mar 9 09:16:01: INFO: Testing the service
Mar 9 09:16:01: INFO: service is tested
</code></pre>
<p>Nice output. It is also worth noting that now you can simply change the functions to modify all of the output of your script. This might be useful if you wanted to change the date format for instance.</p>
<p>For more information on date formatting see <a href="http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/">http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/</a></p>
<p><a href="http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/" title="Bash Best(ish) practices part 4">Bash Best(ish) practices part 4</a><br />
<a href="http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/" title="Bash Best(ish) practices part 2">Bash Best(ish) practices part 2</a><br />
<a href="http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/" title="Bash Best(ish) practices part 1">Bash Best(ish) practices part 1</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/149/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=149&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/03/24/bash-bestish-practices-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Bash Best(ish) practices part 2</title>
		<link>http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/</link>
		<comments>http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/#comments</comments>
		<pubDate>Sun, 17 Mar 2013 16:25:24 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=148</guid>
		<description><![CDATA[Part 2 of our series has us beginning to use functions as a part of script development. Bash functions are pretty simple, and much has already been written about them. Today we will primarly be concerned with how they can &#8230; <a href="http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=148&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Part 2 of our series has us beginning to use functions as a part of script development. Bash functions are pretty simple, and much has already been written about them. Today we will primarly be concerned with how they can help your script flow better and potentially speed up development and troubleshooting.</p>
<p>Lets start with some code…</p>
<pre class="prettyprint"><code>#!/bin/bash
# Copy some files

# Print usage information and exit
print_usage(){
    echo -e "\n" \
    "usage: ./copy_stuff -s sourcedir -d destdir \n" \
    "\n" \
    "-s &lt;sourcedir&gt; source directory\n" \
    "-d &lt;destdir&gt;   destination directory\n" \
    "-h             this help\n" \
    "\n" &amp;&amp; exit 1
}

# Loop through $@ to find flags
while getopts ":hs:d:" FLAG; do
    case "${FLAG}" in
        s) # Our source
            SOURCE="${OPTARG}" ;;
        d) # Our destination
            DEST="${OPTARG}" ;;
        h) # Print usage information
            HELP=1;;
        [:?]) # Print usage information
            print_usage;;
    esac
done

# Copy some files from one place to another
copy_files(){
    echo "Copying ${SOURCE}/foo to ${DEST}/foo"
    echo "Copying ${SOURCE}/bar to ${DEST}/bar"
    echo "Copying ${SOURCE}/baz/foo to ${DEST}/baz/foo"
    echo "Copying ${SOURCE}/baz/bar/foo to ${DEST}/baz/bar/foo"
    echo "Copying ${SOURCE}/dir/somefile to ${DEST}/dir/someotherfile"
}

# Restart some service
restart_service(){
    echo "Stopping the service"
    echo "Making sure the service is stopped"
    echo "Reticulating splines..."
    echo "Starting the service"
}

# Test to see if the service is running
test_service(){
    echo "service is tested"
}

[ "${HELP}" ] &amp;&amp; print_usage

if [ ${SOURCE} ] &amp;&amp; [ ${DEST} ]
then
    echo "Copying files from ${SOURCE} to ${DEST}"
    copy_files
    echo "Restarting the service"
    restart_service
    echo "Testing the service"
    test_service
fi
</code></pre>
<p>There is a lot happening in this script, but you can see that we are expanding upon ideas from last week.</p>
<p>Lets break down the key elements.</p>
<pre class="prettyprint"><code>print_usage(){
    echo -e "\n" \
    "usage: ./copy_stuff -s sourcedir -d destdir \n" \
    "\n" \
    "-s &lt;sourcedir&gt; source directory\n" \
    "-d &lt;destdir&gt;   destination directory\n" \
    "-h             this help\n" \
    "\n" &amp;&amp; exit 1
}
</code></pre>
<p>We define a function called print_usage which, you guessed it, prints the usage information for our script. I cannot understate how usefull this is when you are expecting your script to be run by other people. Please don&#8217;t write scripts that by default just do something or use the -h flag to perform some operation other than printing usage.</p>
<p>The format used here allows for more readable help.</p>
<pre class="prettyprint"><code>[:?]) # Print usage information
    print_usage;;
</code></pre>
<p>The second thing you should notice is that we have added a new option check. : and ? are triggered if you fail to provide an argument to a flag that requires one or you provide a flag that does not exist. In this case we want to let the user know that they do not know how to invoke our program and they should read the &#8220;friendly&#8221; manual.</p>
<pre class="prettyprint"><code>[ "${HELP}" ] &amp;&amp; print_usage
</code></pre>
<p>This line was added to preempt other operations from taking place if you have specified the -h flag.</p>
<pre class="prettyprint"><code># Copy some files from one place to another
copy_files(){
    echo "Copying ${SOURCE}/foo to ${DEST}/foo"
    echo "Copying ${SOURCE}/bar to ${DEST}/bar"
    echo "Copying ${SOURCE}/baz/foo to ${DEST}/baz/foo"
    echo "Copying ${SOURCE}/baz/bar/foo to ${DEST}/baz/bar/foo"
    echo "Copying ${SOURCE}/dir/somefile to ${DEST}/dir/someotherfile"
}

# Restart some service
restart_service(){
    echo "Stopping the service"
    echo "Making sure the service is stopped"
    echo "Reticulating splines..."
    echo "Starting the service"
}

# Test to see if the service is running
test_service(){
    echo "service is tested"
}
</code></pre>
<p>The next thing you should see is that we put all of the work that the script is doing within function declarations. As you will see below, this allows us to implement and test each component separately.</p>
<pre class="prettyprint"><code>if [ ${SOURCE} ] &amp;&amp; [ ${DEST} ]
then
    echo "Copying files from ${SOURCE} to ${DEST}"
    copy_files
    echo "Restarting the service"
    restart_service
    echo "Testing the service"
    test_service
fi
</code></pre>
<p>It should also become apparent that you can more easily wrap function calls in argument checks which can help your script be more readable. If we want to perform work on the test service function, we can simply comment out the other three calls.</p>
<p>Let&#8217;s see this script in action.</p>
<pre class="prettyprint"><code># ./copy_stuff -h

 usage: ./copy_stuff -s sourcedir -d destdir

 -s &lt;sourcedir&gt; source directory
 -d &lt;destdir&gt;   destination directory
 -h             this help 

# ./copy_stuff -h -s /dir1

 usage: ./copy_stuff -s sourcedir -d destdir

 -s &lt;sourcedir&gt; source directory
 -d &lt;destdir&gt;   destination directory
 -h             this help

# ./copy_stuff -h -s /dir1 -d /dir2

 usage: ./copy_stuff -s sourcedir -d destdir

 -s &lt;sourcedir&gt; source directory
 -d &lt;destdir&gt;   destination directory
 -h             this help

# ./copy_stuff -s /dir1 -d /dir2
Copying files from /dir1 to /dir2
Copying /dir1/foo to /dir2/foo
Copying /dir1/bar to /dir2/bar
Copying /dir1/baz/foo to /dir2/baz/foo
Copying /dir1/baz/bar/foo to /dir2/baz/bar/foo
Copying /dir1/dir/somefile to /dir2/dir/someotherfile
Restarting the service
Stopping the service
Making sure the service is stopped
Reticulating splines...
Starting the service
Testing the service
service is tested
</code></pre>
<p>For more information regarding functions see <a href="http://www.cyberciti.biz/faq/bash-shell-script-function-examples/">http://www.cyberciti.biz/faq/bash-shell-script-function-examples/</a></p>
<p><a href="http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/" title="Bash Best(ish) practices part 4">Bash Best(ish) practices part 4</a><br />
<a href="http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-3/" title="Bash Best(ish) practices part 3">Bash Best(ish) practices part 3</a><br />
<a href="http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/" title="Bash Best(ish) practices part 1">Bash Best(ish) practices part 1</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=148&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>Bash Best(ish) practices part 1</title>
		<link>http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/</link>
		<comments>http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 13:55:28 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Food for Thought on scriptogr.am]]></category>
		<category><![CDATA[IFTTT]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=147</guid>
		<description><![CDATA[Hello again. It has been some time since I have posted here. Bash is the focus of todays post. Well… Really it is the focus of several posts over the next few weeks as we delve into some best(ish) practices &#8230; <a href="http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=147&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Hello again. It has been some time since I have posted here. Bash is the focus of todays post. Well… Really it is the focus of several posts over the next few weeks as we delve into some best(ish) practices when writing bash scripts. The culmnination of these articles will be the creation of a bash script template file that can be used to make writing scripts a little faster/better.</p>
<p>Enter getopts… We will start with some code then break it down.</p>
<pre class="prettyprint"><code># Loop through $@ to find flags
while getopts ":hs:e:" FLAG; do
    case "${FLAG}" in
        s) # Here lies some flag
            OPTION1="${OPTARG}" ;;
        e) # Another flag, we will call this one fred
            FRED="${OPTARG}" ;;
        h) # A flag with no value
            HELP=1
            echo "You need some help. We will get to that later." ;;
    esac
done
</code></pre>
<p>Let&#8217;s break it down…</p>
<pre class="prettyprint"><code>getopts ":hs:e:" FLAG
</code></pre>
<p>The format of getopts is</p>
<pre class="prettyprint"><code>getopts OPTSTRING VARNAME
</code></pre>
<p>Where:</p>
<p>OPTSTRING &#8211; is what getopts should expect as an option and whether to expect an argument to that option. It also allows the specification of how to deal with errors.</p>
<p>VARNAME &#8211; is used to report back the option that was found.</p>
<p>Options are specified as simply the character you are looking for(sorry no long options supported). If you want to retreive the argument immediatly following the flag, you may place a : after the option.</p>
<p>Error handling defaults to verbose and can be switched to silent by preceding the OPTSTRING with a :</p>
<p>In the event of an error, VARNAME will be set to ? to indicate an invalid option and : to indicate that a required argument was not found.</p>
<p>getopts is usually run in a while loop so that it can process all of $@.</p>
<pre class="prettyprint"><code>case "${FLAG}" in
</code></pre>
<p>A case statement is used to evaluate which optino we are dealing with, although you could use if/then if you so chose.</p>
<pre class="prettyprint"><code>s) # Here lies some flag
    OPTION1="${OPTARG}" ;;
e) # Another flag, we will call this one fred
    FRED="${OPTARG}" ;;
h) # A flag with no value
    HELP=1
    echo "You need some help. We will get to that later." ;;
</code></pre>
<p>You can perform whatever operations you want based on the option chosen, although it is common to either set a variable or run a function such as print_usage.</p>
<p>How do we put this together into something useful?</p>
<pre class="prettyprint"><code># ./foo -h
You need some help. We will get to that later.
# ./foo -s hello
hello world
# ./foo -s
# ./foo -s goodby
#
</code></pre>
<p>For more information see <a href="http://wiki.bash-hackers.org/howto/getopts_tutorial">http://wiki.bash-hackers.org/howto/getopts_tutorial</a></p>
<p>Have fun…</p>
<p><a href="http://jeredsutton.com/2013/03/29/bash-bestish-practices-part-4/" title="Bash Best(ish) practices part 4">Bash Best(ish) practices part 4</a><br />
<a href="http://jeredsutton.com/2013/03/17/bash-bestish-practices-part-3/" title="Bash Best(ish) practices part 3">Bash Best(ish) practices part 3</a><br />
<a href="http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-2/" title="Bash Best(ish) practices part 2">Bash Best(ish) practices part 2</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=147&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2013/03/09/bash-bestish-practices-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
		<item>
		<title>lighttpd fails to start!!!???!!!</title>
		<link>http://jeredsutton.com/2012/06/10/lighttpd-fails-to-start/</link>
		<comments>http://jeredsutton.com/2012/06/10/lighttpd-fails-to-start/#comments</comments>
		<pubDate>Sun, 10 Jun 2012 12:08:03 +0000</pubDate>
		<dc:creator>JeredSutton</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[enterprise-it]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://jeredsutton.com/?p=132</guid>
		<description><![CDATA[I logged into a legacy system a few weeks ago, because the web server that was serving an application had ceased performing its function. I quickly noticed that the server program that was running was lighttpd as opposed to apache. &#8230; <a href="http://jeredsutton.com/2012/06/10/lighttpd-fails-to-start/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=132&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I logged into a legacy system a few weeks ago, because the web server that was serving an application had ceased performing its function. I quickly noticed that the server program that was running was lighttpd as opposed to apache. Not that I care mind you, but I do tend to have more experience with apache than lighttpd. If you want to skip to the solution, move to &#8220;eureka&#8221; below.</p>
<p>In any case, I attempted to perform a stop/start on the server to see if that would resolve the issue. The stop succeeded(or so I thought) but the start failed. I ran a quick &#8216;ps&#8217; to see what was going on and found that the server was still running. Undeterred, I killed the server and ran the start-up script again and it worked. I thought that maybe something must be wrong with the init script. Perhaps this version of lighttpd was installed manually(it happens). I tried running the stop section manually and found that the stop command was unable to locate the pid of the running process. The strange part is that the pid file existed but it did not reflect the pid returned by &#8216;ps&#8217;.</p>
<p>I performed some research(googled it) and found several individuals that had encountered similar issues. Unfortunately, in the true spirit of opensource they just changed the init script to kill -9. This is of course an unacceptable solution unless you like bringing a nuclear weapon to a knife fight so I continued my search looking at the default config files.</p>
<p>Eureka!</p>
<pre>
server.pid-file            = "/var/run/lighttpd.pid"
</pre>
<p>Apparently, the process that starts lighttpd performs some magic with the pid that results in this line being needed.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jeredsutton.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jeredsutton.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jeredsutton.com&#038;blog=13705854&#038;post=132&#038;subd=jeredsutton&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jeredsutton.com/2012/06/10/lighttpd-fails-to-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c50e3b159d5985e8dc06839b39e381c9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jsuttonlogin</media:title>
		</media:content>
	</item>
	</channel>
</rss>
