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"}" >&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" >&2
  124. clean_up
  125. exit
  126. ;;
  127. TERM)
  128. echo "$PROGNAME: Program terminated" >&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 <<EOF
  189.  
  190. ${PROGNAME} ${VERSION}
  191.  
  192. This is a program to ${DESCRIPTION}.
  193.  
  194. $(usage)
  195.  
  196. Options:
  197.  
  198. -h, --help Display this help message and exit.
  199. --version Display program version.
  200.  
  201. Arguments:
  202.  
  203. ARGUMENT1 The first argument.
  204. ARGUMENT2 The second argument.
  205.  
  206. #NOTE: You must be the superuser to run this script.
  207. #WARNING! Contains security info. Do not set world-readable.
  208. EOF
  209.  
  210. } # end of helptext
  211.  
  212.  
  213. function root_check() {
  214.  
  215. # ------------------------------------------------------------------
  216. # Function to check if user is root
  217. # No arguments
  218. # ------------------------------------------------------------------
  219.  
  220. if [ "$(id | sed 's/uid=\([0-9]*\).*/\1/')" != "0" ]; then
  221. error_exit "You must be the superuser to run this script."
  222. fi
  223.  
  224. } # end of root_check
  225.  
  226.  
  227. function dummy_function1() {
  228.  
  229. # ------------------------------------------------------------------
  230. # Function to [INSERT FUNCTION DESCRIPTION]
  231. # No arguments
  232. # ------------------------------------------------------------------
  233.  
  234. return
  235.  
  236. } # end of dummy_function1
  237.  
  238.  
  239. function dummy_function2() {
  240.  
  241. # ------------------------------------------------------------------
  242. # Function to [INSERT FUNCTION DESCRIPTION]
  243. # Arguments:
  244. # 1 (optional) arg1
  245. # ------------------------------------------------------------------
  246.  
  247. return
  248.  
  249. } # end of dummy_function2
  250.  
  251.  
  252. function dummy_function3() {
  253.  
  254. # ------------------------------------------------------------------
  255. # Function to [INSERT FUNCTION DESCRIPTION]
  256. # Arguments:
  257. # 1 (required) arg1
  258. # 2 (optional) arg2
  259. # ------------------------------------------------------------------
  260.  
  261. # Fatal error if required arguments are missing
  262.  
  263. if [ "$1" = "" ]; then
  264. error_exit "dummy_function3: missing argument 1"
  265. fi
  266.  
  267. return
  268.  
  269. } # end of dummy_function3
  270.  
  271.  
  272. function dummy_function4() {
  273.  
  274. # ------------------------------------------------------------------
  275. # Function to [INSERT FUNCTION DESCRIPTION]
  276. # Arguments:
  277. # 1 (required) arg1
  278. # 2 (required) arg2
  279. # ------------------------------------------------------------------
  280.  
  281. # Fatal error if required arguments are missing
  282.  
  283. if [ "$1" = "" ]; then
  284. error_exit "dummy_function4: missing argument 1"
  285. fi
  286. if [ "$2" = "" ]; then
  287. error_exit "dummy_function4: missing argument 2"
  288. fi
  289.  
  290. return
  291.  
  292. } # end of dummy_function4
  293.  
  294.  
  295. # ------------------------------------------------------------------
  296. # Program starts here
  297. # ------------------------------------------------------------------
  298.  
  299. ##### Initialization And Setup #####
  300.  
  301. ## Set file creation mask so that all files are created with 600
  302. ## permissions.
  303. ##
  304. #umask 066
  305. #root_check
  306.  
  307. # Trap TERM, HUP, and INT signals and properly exit
  308.  
  309. trap "signal_exit TERM" TERM HUP
  310. trap "signal_exit INT" INT
  311.  
  312. ## Create temporary file(s)
  313. #
  314. #make_temp_files
  315.  
  316.  
  317. ##### Command Line Processing #####
  318.  
  319. # # if at least one argument is required...
  320. # if [ $# -eq 0 ]; then
  321. # echo "This is ${PROGNAME} ${VERSION}"
  322. # usage
  323. # clean_up
  324. # exit 1
  325. # fi
  326.  
  327.  
  328. # # simple command line parsing with getopts buildin instead of external getopt
  329. # # if only -h|--help option is needed
  330. # if [ "$1" = "--help" ]; then
  331. # helptext
  332. # graceful_exit
  333. # fi
  334.  
  335. # while getopts :h OPT; do
  336. # case $OPT in
  337. # h)
  338. # helptext
  339. # graceful_exit
  340. # ;;
  341. # *)
  342. # usage
  343. # clean_up
  344. # exit 1
  345. # esac
  346. # done
  347.  
  348.  
  349. # Note that we use `"$@"' to let each command-line parameter expand to
  350. # a separate word. The quotes around `$@' are essential! We need
  351. # GETOPT_TEMP as the `eval set --' would nuke the return value of
  352. # getopt.
  353. GETOPT_TEMP=$(getopt -o +ab:c::h --long a_option,b_option:,c_option::,help,version -n "$PROGNAME" -- "$@")
  354.  
  355. if [ $? != 0 ] ; then
  356. error_exit "Error parsing command line. Terminating..."
  357. fi
  358.  
  359. # Note the quotes around `$GETOPT_TEMP': they are essential!
  360. eval set -- "$GETOPT_TEMP"
  361.  
  362. # no error checking necessary; sanity of command line and required
  363. # arguments has been checked by getopt program
  364. while true ; do
  365. case $1 in
  366. -a|--a_option)
  367. echo "a_option" ;
  368. shift
  369. ;;
  370. -b|--b_option)
  371. echo "b_option - argument: $2"
  372. shift 2
  373. ;;
  374. -c|--c_option)
  375. # c has an optional argument. As we are in quoted mode,
  376. # an empty parameter will be generated if its optional
  377. # argument is not found.
  378. case "$2" in
  379. "")
  380. echo "c_option - no argument" ;
  381. shift 2
  382. ;;
  383. *)
  384. echo "c_option - argument: $2" ;
  385. shift 2
  386. ;;
  387. esac
  388. ;;
  389. -h|--help)
  390. helptext ;
  391. graceful_exit
  392. ;;
  393. --version)
  394. echo "${PROGNAME} ${VERSION}" ;
  395. shift
  396. ;;
  397. --)
  398. shift ;
  399. break
  400. ;;
  401. *)
  402. # should be impossible to reach: getopt should have caught
  403. # an error
  404. error_exit "This should not have happened; unknown option '$1'. Terminating..."
  405. ;;
  406. esac
  407. done
  408. unset GETOPT_TEMP
  409.  
  410. # processing remaining arguments for the client
  411. if [ $# -ne 0 ]; then
  412. echo "Remaining arguments: $@"
  413. fi
  414.  
  415.  
  416. ##### Main Logic #####
  417.  
  418. echo "Hello World"
  419.  
  420. graceful_exit
  421.  
  422. # end of bash_skeleton.sh
AttachmentSize
bash_skeleton.sh.txt10.45 KB

Post new comment