Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Template to start a simple bash program. This is designed only for the |
| 4 | # simplest of programs where all program paramters are positional, there is no |
| 5 | # help text, etc. |
| 6 | |
| 7 | # Description of argument(s): |
| 8 | # parm1 Bla, bla, bla (e.g. "example data"). |
| 9 | |
| 10 | |
| 11 | ############################################################################### |
| 12 | function get_parms { |
| 13 | |
| 14 | # Get program parms. |
| 15 | |
| 16 | parm1="${1}" ; shift |
| 17 | |
| 18 | return 0 |
| 19 | |
| 20 | } |
| 21 | ############################################################################### |
| 22 | |
| 23 | |
| 24 | ############################################################################### |
| 25 | function validate_parms { |
| 26 | |
| 27 | # Validate program parameters. |
| 28 | |
| 29 | # Your validation code here. |
| 30 | |
| 31 | if [ -z "${parm1}" ] ; then |
| 32 | echo "**ERROR** You must provide..." >&2 |
| 33 | return 1 |
| 34 | fi |
| 35 | |
| 36 | return 0 |
| 37 | |
| 38 | } |
| 39 | ############################################################################### |
| 40 | |
| 41 | |
| 42 | ############################################################################### |
| 43 | function mainf { |
| 44 | |
| 45 | # We create a mainf for a couple of reasons: |
| 46 | # The coding rules in a template are slightly different than for the true |
| 47 | # mainline. We wish to eliminate those inconsistencies. Examples: |
| 48 | # - The "local" builtin is allowed in functions but not in the mainline. |
| 49 | # - It is good practice to have functions return 1 rather than exit 1. |
| 50 | # return is not valid in the mainline. Again, we don't want to have to |
| 51 | # care when we code things about whether we are in the mainline or a |
| 52 | # function. |
| 53 | |
| 54 | get_parms "$@" || return 1 |
| 55 | |
| 56 | validate_parms || return 1 |
| 57 | |
| 58 | # Your code here... |
| 59 | |
| 60 | return 0 |
| 61 | |
| 62 | } |
| 63 | ############################################################################### |
| 64 | |
| 65 | |
| 66 | ############################################################################### |
| 67 | # Main |
| 68 | |
| 69 | mainf "${@}" |
| 70 | rc="${?}" |
| 71 | exit "${rc}" |
| 72 | |
| 73 | ############################################################################### |