I commonly write a quick bash script to solve certain problems. I found myself typing the same code over and over again. Looking for a better solution I googled for a bash script skeleton and found a good one at withouthat.org. I customized it a bit and updated it. Here is my version.
#!/bin/bash # ------------------------------------------------------------------ # # Shell program to [INSERT DESCRIPTION]. # # Copyright 2008, Steve Francia <steve.francia+skel@gmail.com>. # # 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. # # Description: # # #NOTE: You must be the superuser to run this script. # #WARNING! Contains security info. Do not set world-readable. # # Usage: # # bash_skeleton.sh [ -h | --help ] [OPTIONS] [ARGUMENTS] # # Options: # # -h, --help Display this help message and exit. # --version Display program version. # # Arguments: # # ARGUMENT1 The first argument. # ARGUMENT2 The second argument. # # External programs: # # getopt - parse command options (enhanced) # mktemp - make temporary filename (unique) # sed - stream editor # basename - strip directory and suffix from filenames # # Revision History: # # 2008-07-25 File created by new_script ver. 2.1.0 # See ChangeLog for additional changes # # # ------------------------------------------------------------------ ##### Preamble ##### # ------------------------------------------------------------------ # Constants # ------------------------------------------------------------------ declare -r PROGNAME=$(basename $0) declare -r VERSION="0.1" declare -r DESCRIPTION="The Program description" ##### Functions ##### # ------------------------------------------------------------------ # Functions # ------------------------------------------------------------------ function clean_up() { # ------------------------------------------------------------------ # Function to remove temporary files and other housekeeping # No arguments # ------------------------------------------------------------------ #rm -f ${TEMP_FILE1} return } # end of clean_up function error_exit() { # ------------------------------------------------------------------ # Function for exit due to fatal program error # Arguments: # 1 (optional) string containing descriptive error message # ------------------------------------------------------------------ echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2 clean_up exit 1 } # end of error_exit function graceful_exit() { # ------------------------------------------------------------------ # Function called for a graceful exit # No arguments # ------------------------------------------------------------------ clean_up exit } # end of graceful_exit function signal_exit() { # ------------------------------------------------------------------ # Function to handle termination signals # Arguments: # 1 (optional) signal_spec # ------------------------------------------------------------------ case $1 in INT) echo "$PROGNAME: Program aborted by user" >&2 clean_up exit ;; TERM) echo "$PROGNAME: Program terminated" >&2 clean_up exit ;; *) error_exit "$PROGNAME: Terminating on unknown signal" ;; esac } # end of signal_exit function make_temp_files() { # ------------------------------------------------------------------ # Function to create temporary files # No arguments # ------------------------------------------------------------------ # Use user's local tmp directory if it exists if [ -d ~/tmp ]; then TEMP_DIR=~/tmp else TEMP_DIR=/tmp fi # Temp file for this script, using paranoid method of creation to # insure that file name is not predictable. This is for security # to avoid "tmp race" attacks. If more files are needed, create # using the same form. TEMP_FILE1=$(mktemp -q "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX") if [ "$TEMP_FILE1" = "" ]; then error_exit "cannot create temp file!" fi } # end of make_temp_files function usage() { # ------------------------------------------------------------------ # Function to display usage message (does not exit) # No arguments # ------------------------------------------------------------------ echo "Usage: ${PROGNAME} [-h | --help] [OPTIONS] [ARGUMENTS]" } # end of usage function helptext() { # ------------------------------------------------------------------ # Function to display help message for program # No arguments # ------------------------------------------------------------------ cat <<eof ${progname}="" ${version}="" this="" is="" a="" program="" to="" ${description}.="" $(usage)="" options:="" -h,="" --help="" display="" help="" message="" and="" exit.="" --version="" version.="" arguments:="" argument1="" the="" first="" argument.="" argument2="" second="" #note:="" you="" must="" be="" superuser="" run="" script.="" #warning!="" contains="" security="" info.="" do="" not="" set="" world-readable.="" eof="" }="" #="" end="" of="" helptext="" function="" root_check()="" {="" ------------------------------------------------------------------="" check="" if="" user="" root="" no="" arguments="" [="" $(id="" |="" sed="" s="" uid="\([0-9]*\).*/\1/')"" !="0" ];="" then="" error_exit="" script."="" fi="" root_check="" dummy_function1()="" [insert="" description]="" return="" dummy_function1="" dummy_function2()="" 1="" (optional)="" arg1="" dummy_function2="" dummy_function3()="" (required)="" 2="" arg2="" fatal="" error="" required="" are="" missing="" $1"="" dummy_function3:="" argument="" 1"="" dummy_function3="" dummy_function4()="" dummy_function4:="" $2"="" 2"="" dummy_function4="" starts="" here="" #####="" initialization="" setup="" ##="" file="" creation="" mask="" so="" that="" all="" files="" created="" with="" 600="" permissions.="" #umask="" 066="" #root_check="" trap="" term,="" hup,="" int="" signals="" properly="" exit="" signal_exit="" term"="" term="" hup="" int"="" create="" temporary="" file(s)="" #make_temp_files="" command="" line="" processing="" at="" least="" one="" required...="" $#="" -eq="" 0="" echo="" ${version}"="" usage="" clean_up="" simple="" parsing="" getopts="" buildin="" instead="" external="" getopt="" only="" -h|--help="" option="" needed="" graceful_exit="" while="" :h="" opt;="" case="" $opt="" in="" h)="" ;;="" *)="" esac="" done="" note="" we="" use="" `"$@"'="" let="" each="" command-line="" parameter="" expand="" separate="" word.="" quotes="" around="" `$@'="" essential!="" need="" getopt_temp="" as="" `eval="" --'="" would="" nuke="" value="" getopt.="" -o="" +ab:c::h="" --long="" a_option,b_option:,c_option::,help,version="" -n="" $progname"="" --="" $@")="" $?="" ]="" ;="" line.="" terminating..."="" `$getopt_temp':="" they="" eval="" $getopt_temp"="" checking="" necessary;="" sanity="" has="" been="" checked="" by="" true="" $1="" -a|--a_option)="" a_option"="" shift="" -b|--b_option)="" b_option="" -="" argument:="" -c|--c_option)="" c="" an="" optional="" quoted="" mode,="" empty="" will="" generated="" its="" found.="" )="" c_option="" argument"="" -h|--help)="" --version)="" --)="" break="" should="" impossible="" reach:="" have="" caught="" happened;="" unknown="" $1'.="" unset="" remaining="" for="" client="" -ne="" $@"="" main="" logic="" hello="" world"="" bash_skeleton.sh=""></eof></steve.francia+skel@gmail.com>














Post new comment