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 this help message and exit. --version Display program version. Arguments: ARGUMENT1 The first argument. ARGUMENT2 The second argument. #NOTE: You must be the superuser to run this script. #WARNING! Contains security info. Do not set world-readable. EOF } # end of helptext function root_check() { # ------------------------------------------------------------------ # Function to check if user is root # No arguments # ------------------------------------------------------------------ if [ "$(id | sed 's/uid=\([0-9]*\).*/\1/')" != "0" ]; then error_exit "You must be the superuser to run this script." fi } # end of root_check function dummy_function1() { # ------------------------------------------------------------------ # Function to [INSERT FUNCTION DESCRIPTION] # No arguments # ------------------------------------------------------------------ return } # end of dummy_function1 function dummy_function2() { # ------------------------------------------------------------------ # Function to [INSERT FUNCTION DESCRIPTION] # Arguments: # 1 (optional) arg1 # ------------------------------------------------------------------ return } # end of dummy_function2 function dummy_function3() { # ------------------------------------------------------------------ # Function to [INSERT FUNCTION DESCRIPTION] # Arguments: # 1 (required) arg1 # 2 (optional) arg2 # ------------------------------------------------------------------ # Fatal error if required arguments are missing if [ "$1" = "" ]; then error_exit "dummy_function3: missing argument 1" fi return } # end of dummy_function3 function dummy_function4() { # ------------------------------------------------------------------ # Function to [INSERT FUNCTION DESCRIPTION] # Arguments: # 1 (required) arg1 # 2 (required) arg2 # ------------------------------------------------------------------ # Fatal error if required arguments are missing if [ "$1" = "" ]; then error_exit "dummy_function4: missing argument 1" fi if [ "$2" = "" ]; then error_exit "dummy_function4: missing argument 2" fi return } # end of dummy_function4 # ------------------------------------------------------------------ # Program starts here # ------------------------------------------------------------------ ##### Initialization And Setup ##### ## Set file creation mask so that all files are created with 600 ## permissions. ## #umask 066 #root_check # Trap TERM, HUP, and INT signals and properly exit trap "signal_exit TERM" TERM HUP trap "signal_exit INT" INT ## Create temporary file(s) # #make_temp_files ##### Command Line Processing ##### # # if at least one argument is required... # if [ $# -eq 0 ]; then # echo "This is ${PROGNAME} ${VERSION}" # usage # clean_up # exit 1 # fi # # simple command line parsing with getopts buildin instead of external getopt # # if only -h|--help option is needed # if [ "$1" = "--help" ]; then # helptext # graceful_exit # fi # while getopts :h OPT; do # case $OPT in # h) # helptext # graceful_exit # ;; # *) # usage # clean_up # exit 1 # esac # done # Note that we use `"$@"' to let each command-line parameter expand to # a separate word. The quotes around `$@' are essential! We need # GETOPT_TEMP as the `eval set --' would nuke the return value of # getopt. GETOPT_TEMP=$(getopt -o +ab:c::h --long a_option,b_option:,c_option::,help,version -n "$PROGNAME" -- "$@") if [ $? != 0 ] ; then error_exit "Error parsing command line. Terminating..." fi # Note the quotes around `$GETOPT_TEMP': they are essential! eval set -- "$GETOPT_TEMP" # no error checking necessary; sanity of command line and required # arguments has been checked by getopt program while true ; do case $1 in -a|--a_option) echo "a_option" ; shift ;; -b|--b_option) echo "b_option - argument: $2" shift 2 ;; -c|--c_option) # c has an optional argument. As we are in quoted mode, # an empty parameter will be generated if its optional # argument is not found. case "$2" in "") echo "c_option - no argument" ; shift 2 ;; *) echo "c_option - argument: $2" ; shift 2 ;; esac ;; -h|--help) helptext ; graceful_exit ;; --version) echo "${PROGNAME} ${VERSION}" ; shift ;; --) shift ; break ;; *) # should be impossible to reach: getopt should have caught # an error error_exit "This should not have happened; unknown option '$1'. Terminating..." ;; esac done unset GETOPT_TEMP # processing remaining arguments for the client if [ $# -ne 0 ]; then echo "Remaining arguments: $@" fi ##### Main Logic ##### echo "Hello World" graceful_exit # end of bash_skeleton.sh
| Attachment | Size |
|---|---|
| bash_skeleton.sh.txt | 10.45 KB |









Post new comment