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 | ||||
George Keishing | 4f5eb80 | 2019-10-25 11:29:32 -0500 | [diff] [blame] | 4 | # simplest of programs where all program parameters are positional, there is no |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 5 | # help text, etc. |
6 | |||||
7 | # Description of argument(s): | ||||
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 8 | # parm1 Bla, bla, bla (e.g. "example data"). |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 9 | |
10 | |||||
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 11 | function get_parms() { |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 12 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 13 | # Get program parms. |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 14 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 15 | parm1="${1}" ; shift |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 16 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 17 | return 0 |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 18 | |
19 | } | ||||
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 20 | |
21 | |||||
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 22 | function exit_function() { |
Michael Walsh | 7beadb2 | 2017-10-26 17:45:12 -0500 | [diff] [blame] | 23 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 24 | return |
Michael Walsh | 7beadb2 | 2017-10-26 17:45:12 -0500 | [diff] [blame] | 25 | |
26 | } | ||||
27 | |||||
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 28 | function validate_parms() { |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 29 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 30 | # Validate program parameters. |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 31 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 32 | # Your validation code here. |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 33 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 34 | if [ -z "${parm1}" ] ; then |
35 | echo "**ERROR** You must provide..." >&2 | ||||
36 | return 1 | ||||
37 | fi | ||||
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 38 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 39 | trap "exit_function $signal \$?" EXIT |
Michael Walsh | 7beadb2 | 2017-10-26 17:45:12 -0500 | [diff] [blame] | 40 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 41 | return 0 |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 42 | |
43 | } | ||||
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 44 | |
45 | |||||
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 46 | function mainf() { |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 47 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 48 | get_parms "$@" || return 1 |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 49 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 50 | validate_parms || return 1 |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 51 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 52 | # Your code here... |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 53 | |
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 54 | return 0 |
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 55 | |
56 | } | ||||
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 57 | |
58 | |||||
Michael Walsh | 7d68d00 | 2017-05-05 16:36:54 -0500 | [diff] [blame] | 59 | # Main |
60 | |||||
Patrick Williams | 90dfee3 | 2022-12-08 06:52:46 -0600 | [diff] [blame] | 61 | mainf "${@}" |
62 | rc="${?}" | ||||
63 | exit "${rc}" |