From 872ce277b1be4f82a5bc47d6bbe63deb5cc9f06e Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Thu, 2 Jan 2014 15:24:49 +0100 Subject: [PATCH] First commit --- CHANGELOG | 2 ++ README.md | 15 ++++++++ template.sh | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 CHANGELOG create mode 100644 README.md create mode 100755 template.sh diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..553628b --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,2 @@ +2014-01-02 +Version 0.1 - Initial release diff --git a/README.md b/README.md new file mode 100644 index 0000000..b14745b --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# shelltemplate # +I noticed that I usually write the same pieces of code at the start of every +project. There's the shebang, the copyright header, the program name and the +version. This is then followed by variables that holds the path to the binaries +used in the script. After that is the sanity check to check for those binaries. +Then there is the function for printing the help screen and usage text. +So I thought it would be a good idea to keep all of this repeating code in +template. And while at it create a neat function to get all the binaries used +in the script into variables without having to manually specifiy the path by +myself. With this template I can simply just specifiy the binaries to be used +by the script and let a loop run through them and set varibles for them. This +neat little function uses the 'which' program. This means that there is only +one hardcoded binary path in the entire script. If the script fails to execute +'which' I just have to locate it manually and update a single variable. + diff --git a/template.sh b/template.sh new file mode 100755 index 0000000..f169588 --- /dev/null +++ b/template.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +################################################################################ +# # +# Copyright (C) 2014 Jack-Benny Persson # +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program; if not, write to the Free Software # +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +# # +################################################################################ + +# template +Version="0.1" +Author="Jack-Benny Persson (jack-benny@cyberinfo.se)" + +# Binaries +Which="/usr/bin/which" + +# Variables + + +### Functions ### + +# Print version information +print_version() +{ + $Printf "\n\n$0 - $Version\n" +} + +# Print help information +print_help() +{ + print_version + $Printf "$Author\n" + $Printf "Shell template\n" + /bin/cat <<-EOT + + Options: + -h + Print detailed help screen + -V + Print version information + -v + Verbose output + -o + Output file + EOT +} + +# Create variables with absolute path to binaries and check +# if we can execute it (binaries will be avaliable in +# variables with first character uppercase, such as Grep) +Binaries=(sed awk egrep mail printf cat grep mktemp rm tail) +Count=0 +for i in ${Binaries[@]}; do + $Which $i &> /dev/null + if [ $? -eq 0 ]; then + declare $(echo ${Binaries[$Count]^}=`${Which} $i`) + ((Count++)) + else + echo "It seems you don't have ${Binaries[$Count]} installed" + exit 1 + fi +done + +# Parse command line options and arguments +while getopts Vvho: Opt; do + case "$Opt" in + h) print_help + exit 0 + ;; + V) print_version + exit 0 + ;; + v) echo "Verbose output" + exit 0 + ;; + o) OutFile=$OPTARG + ;; + *) short_help + exit 1 + ;; + esac +done + + +### Main ### + + + +exit 0