title: Shell script common template
body: <p>Millions of developers write shell scripts to solve various types of tasks. I use shell scripts to simplify deployment, life-cycle management, installation or simply as a <a href="http://en.wikipedia.org/wiki/Glue_language">glue language</a>.</p>
  
  <p>What I've noticed is nobody actually cares about shell scripts style and quality. A lot of teams spend many hours fixing Java, C++, ... style issues, but totally ignore issues in their shell scripts. By the way, usually there is no standard way to implement a shell script within a particular project, so the one may find dozens different, ugly and buggy scripts, spread around the codebase.</p>
  
  <p>To overcome that issue in my projects I decided to create a shell script template, universal and good enough. I will provide my templates as is to make this question a bit more useful. Out of the box these templates provides:</p>
  
  <ul>
  <li>command-line arguments handling</li>
  <li>synchronization</li>
  <li>some basic help</li>
  </ul>
  
  <p><strong>Arguments handling: getopts</strong> (latest version: <strong><a href="https://github.com/RenatGilmanov/shell-script-template">shell-script-template@github</a></strong>)</p>
  
  <pre class="lang-bash prettyprint-override"><code>#!/bin/bash
  # ------------------------------------------------------------------
  # [Author] Title
  #          Description
  # ------------------------------------------------------------------
  
  VERSION=0.1.0
  SUBJECT=some-unique-id
  USAGE="Usage: command -ihv args"
  
  # --- Options processing -------------------------------------------
  if [ $# == 0 ] ; then
      echo $USAGE
      exit 1;
  fi
  
  while getopts ":i:vh" optname
    do
      case "$optname" in
        "v")
          echo "Version $VERSION"
          exit 0;
          ;;
        "i")
          echo "-i argument: $OPTARG"
          ;;
        "h")
          echo $USAGE
          exit 0;
          ;;
        "?")
          echo "Unknown option $OPTARG"
          exit 0;
          ;;
        ":")
          echo "No argument value for option $OPTARG"
          exit 0;
          ;;
        *)
          echo "Unknown error while processing options"
          exit 0;
          ;;
      esac
    done
  
  shift $(($OPTIND - 1))
  
  param1=$1
  param2=$2
  
  # --- Locks -------------------------------------------------------
  LOCK_FILE=/tmp/$SUBJECT.lock
  if [ -f "$LOCK_FILE" ]; then
     echo "Script is already running"
     exit
  fi
  
  trap "rm -f $LOCK_FILE" EXIT
  touch $LOCK_FILE
  
  # --- Body --------------------------------------------------------
  #  SCRIPT LOGIC GOES HERE
  echo $param1
  echo $param2
  # -----------------------------------------------------------------
  </code></pre>
  
  <p><a href="http://code.google.com/p/shflags/">Shell Flags</a> (shFlags) allows to simplify command-line arguments handling a lot, so at some moment of time I decided not to ignore such possibility. </p>
  
  <p><strong>Arguments handling: shflags</strong> (latest version: <strong><a href="https://github.com/RenatGilmanov/shell-script-template">shell-script-template@github</a></strong>)</p>
  
  <pre class="lang-bash prettyprint-override"><code>#!/bin/bash
  # ------------------------------------------------------------------
  # [Author] Title
  #          Description
  #
  #          This script uses shFlags -- Advanced command-line flag
  #          library for Unix shell scripts.
  #          http://code.google.com/p/shflags/
  #
  # Dependency:
  #     http://shflags.googlecode.com/svn/trunk/source/1.0/src/shflags
  # ------------------------------------------------------------------
  VERSION=0.1.0
  SUBJECT=some-unique-id
  USAGE="Usage: command -hv args"
  
  # --- Option processing --------------------------------------------
  if [ $# == 0 ] ; then
      echo $USAGE
      exit 1;
  fi
  
  . ./shflags
  
  DEFINE_string 'aparam' 'adefault' 'First parameter'
  DEFINE_string 'bparam' 'bdefault' 'Second parameter'
  
  # parse command line
  FLAGS "$@" || exit 1
  eval set -- "${FLAGS_ARGV}"
  
  shift $(($OPTIND - 1))
  
  param1=$1
  param2=$2
  
  # --- Locks -------------------------------------------------------
  LOCK_FILE=/tmp/${SUBJECT}.lock
  
  if [ -f "$LOCK_FILE" ]; then
  echo "Script is already running"
  exit
  fi
  
  trap "rm -f $LOCK_FILE" EXIT
  touch $LOCK_FILE
  
  # -- Body ---------------------------------------------------------
  #  SCRIPT LOGIC GOES HERE
  echo "Param A: $FLAGS_aparam"
  echo "Param B: $FLAGS_bparam"
  echo $param1
  echo $param2
  # -----------------------------------------------------------------
  </code></pre>
  
  <p>I do think these templates can be improved to simplify developer's life even more. </p>
  
  <p><strong>So the question is how to improve them to have the following:</strong></p>
  
  <ul>
  <li><strong>built-in logging</strong></li>
  <li><strong>better error handling</strong></li>
  <li><strong>better portability</strong></li>
  <li><strong>smaller footprint</strong></li>
  <li><strong>built-in execution time tracking</strong></li>
  </ul>