The UNIX way.

Around the clock, across the globe. By Vladimir Legeza

Archive for May 2012

Bash script template

leave a comment »

Probably it will be a good idea not to write every script from a blank page, because the most scripts at the beginning contains the same information. like “what this pease of code does”, “who wrote it and when”, “default options declarations” and etc.

In following template I’ll define the most popular parts that all good scripts should contain. Also I figured out that adding --debug option is a very usefull thing that allows you to deep dive throught the code without extra manipulations.

Simple, clean, usefull:

#!/usr/bin/env bash
# Description:
# Execution frequency:
# Options:
# Author: Name e-mail DD/MM/YYYY
# Editions:

echo “Usage:

ARGS=`getopt -o h --long help,debug -- $@`
eval set -- $ARGS

while true ; do
case “$1” in
-h| --help) usage; exit 0;;
--debug) set -x; shift;;
--) shift ; break ;;
*) echo “Internal error!“; usage; exit 1;;

exit 0

Written by Vladimir Legeza

May 30, 2012 at 12:06 pm