Bash Script Skeleton

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.

  1. #!/bin/bash
  2.  
  3. # ------------------------------------------------------------------
  4. #
  5. # Shell program to [INSERT DESCRIPTION].
  6. #
  7. # Copyright 2008, Steve Francia <steve.francia+skel@gmail.com>.
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License as
  11. # published by the Free Software Foundation; either version 2 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. # Description:
  20. #
  21. # #NOTE: You must be the superuser to run this script.
  22. # #WARNING! Contains security info. Do not set world-readable.
  23. #
  24. # Usage:
  25. #
  26. # bash_skeleton.sh [ -h | --help ] [OPTIONS] [ARGUMENTS]
  27. #
  28. # Options:
  29. #
  30. # -h, --help Display this help message and exit.
  31. # --version Display program version.
  32. #
  33. # Arguments:
  34. #
  35. # ARGUMENT1 The first argument.
  36. # ARGUMENT2 The second argument.
  37. #
  38. # External programs:
  39. #
  40. # getopt - parse command options (enhanced)
  41. # mktemp - make temporary filename (unique)
  42. # sed - stream editor
  43. # basename - strip directory and suffix from filenames
  44. #
  45. # Revision History:
  46. #
  47. # 2008-07-25 File created by new_script ver. 2.1.0
  48. # See ChangeLog for additional changes
  49. #
  50. #
  51. # ------------------------------------------------------------------
  52.  
  53. ##### Preamble #####
  54.  
  55. # ------------------------------------------------------------------
  56. # Constants
  57. # ------------------------------------------------------------------
  58.  
  59. declare -r PROGNAME=$(basename $0)
  60. declare -r VERSION="0.1"
  61. declare -r DESCRIPTION="The Program description"
  62.  
  63.  
  64. ##### Functions #####
  65.  
  66. # ------------------------------------------------------------------
  67. # Functions
  68. # ------------------------------------------------------------------
  69.  
  70.  
  71. function clean_up() {
  72.  
  73. # ------------------------------------------------------------------
  74. # Function to remove temporary files and other housekeeping
  75. # No arguments
  76. # ------------------------------------------------------------------
  77.  
  78. #rm -f ${TEMP_FILE1}
  79. return
  80.  
  81. } # end of clean_up
  82.  
  83.  
  84. function error_exit() {
  85.  
  86. # ------------------------------------------------------------------
  87. # Function for exit due to fatal program error
  88. # Arguments:
  89. # 1 (optional) string containing descriptive error message
  90. # ------------------------------------------------------------------
  91.  
  92.  
  93. echo "${PROGNAME}: ${1:-"Unknown Error"}" &gt;&amp;2
  94. clean_up
  95. exit 1
  96.  
  97. } # end of error_exit
  98.  
  99.  
  100. function graceful_exit() {
  101.  
  102. # ------------------------------------------------------------------
  103. # Function called for a graceful exit
  104. # No arguments
  105. # ------------------------------------------------------------------
  106.  
  107. clean_up
  108. exit
  109.  
  110. } # end of graceful_exit
  111.  
  112.  
  113. function signal_exit() {
  114.  
  115. # ------------------------------------------------------------------
  116. # Function to handle termination signals
  117. # Arguments:
  118. # 1 (optional) signal_spec
  119. # ------------------------------------------------------------------
  120.  
  121. case $1 in
  122. INT)
  123. echo "$PROGNAME: Program aborted by user" &gt;&amp;2
  124. clean_up
  125. exit
  126. ;;
  127. TERM)
  128. echo "$PROGNAME: Program terminated" &gt;&amp;2
  129. clean_up
  130. exit
  131. ;;
  132. *)
  133. error_exit "$PROGNAME: Terminating on unknown signal"
  134. ;;
  135.  
  136. esac
  137.  
  138. } # end of signal_exit
  139.  
  140.  
  141. function make_temp_files() {
  142.  
  143. # ------------------------------------------------------------------
  144. # Function to create temporary files
  145. # No arguments
  146. # ------------------------------------------------------------------
  147.  
  148. # Use user's local tmp directory if it exists
  149.  
  150. if [ -d ~/tmp ]; then
  151. TEMP_DIR=~/tmp
  152. else
  153. TEMP_DIR=/tmp
  154. fi
  155.  
  156. # Temp file for this script, using paranoid method of creation to
  157. # insure that file name is not predictable. This is for security
  158. # to avoid "tmp race" attacks. If more files are needed, create
  159. # using the same form.
  160.  
  161. TEMP_FILE1=$(mktemp -q "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX")
  162. if [ "$TEMP_FILE1" = "" ]; then
  163. error_exit "cannot create temp file!"
  164. fi
  165.  
  166. } # end of make_temp_files
  167.  
  168.  
  169. function usage() {
  170.  
  171. # ------------------------------------------------------------------
  172. # Function to display usage message (does not exit)
  173. # No arguments
  174. # ------------------------------------------------------------------
  175.  
  176. echo "Usage: ${PROGNAME} [-h | --help] [OPTIONS] [ARGUMENTS]"
  177.  
  178. } # end of usage
  179.  
  180.  
  181. function helptext() {
  182.  
  183. # ------------------------------------------------------------------
  184. # Function to display help message for program
  185. # No arguments
  186. # ------------------------------------------------------------------
  187.  
  188. cat &lt;<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/')&quot;" !="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

The content of this field is kept private and will not be shown publicly.