Skip to content

Commit 47d1436

Browse files
authored
Merge pull request #1 from richbl/dev
Performance/compliance improvements
2 parents 784a1d3 + 67e897e commit 47d1436

3 files changed

Lines changed: 97 additions & 81 deletions

File tree

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,28 @@
44

55
**Bash-Lib** is a library of common [bash](https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) routines used in projects based on the [a-bash-template (BaT)](https://github.com/richbl/a-bash-template) project. This library is broken into two files:
66

7-
- `General`: Routines created to check program and file dependencies, banner display, *etc*.
8-
- `Args`: Routines created to manage command-line argument parsing
7+
- `general`: Routines created to:
8+
- Check program dependencies
9+
- Check file dependencies
10+
- Format and display a program banner, e.g.,
11+
>>
12+
|
13+
| A bash template (BaT) to ease argument parsing and management
14+
| 0.2.0
15+
|
16+
| Usage:
17+
| bash_template.sh -a alpha -b bravo [-c charlie] -d delta
18+
|
19+
| -a, --alpha alpha (something descriptive)
20+
| -b, --bravo bravo (something descriptive)
21+
| -c, --charlie charlie (this is optional)
22+
| -d, --delta delta (something descriptive)
23+
|
24+
25+
- `args`: Routines created to:
26+
- Parse a JSON file for program configuration details
27+
- Scan command-line arguments for accuracy and completeness
28+
29+
For details on how the various routines of this library project are used, see the [A-Bash-Template (BaT)](https://github.com/richbl/a-bash-template#a-bash-template) project.
30+
31+
> Note that this project is managed as a Git [submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) project specifically to keep projects that use this library up-to-date without manual intervention.

args

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,22 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
44
# -----------------------------------------------------------------------------
55
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
66
#
7-
# This program is free software: you can redistribute it and/or modify
8-
# it under the terms of the GNU General Public License as published by
9-
# the Free Software Foundation, either version 3 of the License, or
10-
# (at your option) any later version.
7+
# This program is free software: you can redistribute it and/or modify it under
8+
# the terms of the GNU General Public License as published by the Free Software
9+
# Foundation, either version 3 of the License, or (at your option) any later
10+
# version.
11+
#
12+
# This program is distributed in the hope that it will be useful, but WITHOUT
13+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
15+
# <http://www.gnu.org/licenses/> for more details.
1116
#
12-
# This program is distributed in the hope that it will be useful,
13-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
# GNU General Public License at <http://www.gnu.org/licenses/> for
16-
# more details.
1717
# -----------------------------------------------------------------------------
1818
#
1919
# bash library for command line argument loading/parsing/validating
2020
#
2121

22-
EXEC_DIR="$(dirname "$0")"
23-
# shellcheck source=bash-lib/general
24-
source "${EXEC_DIR}/bash-lib/general"
25-
26-
ARGS_FILE="${EXEC_DIR}/data/config.json"
22+
readonly ARGS_FILE="data/config.json"
2723

2824
# -----------------------------------------------------------------------------
2925
# query data/config.json, returning the details object
@@ -40,7 +36,7 @@ function get_config_arg {
4036
}
4137

4238
# -----------------------------------------------------------------------------
43-
# query data/config.json, returning the arguments object length
39+
# query data/config.json, returning the count of arguments
4440
#
4541
function get_config_args_length {
4642
printf "%s" "$(jq -r '.arguments|length' < "${ARGS_FILE}")"
@@ -51,20 +47,18 @@ function get_config_args_length {
5147
#
5248
function get_config_arg_value {
5349

54-
local COUNT_ARGS=0
55-
COUNT_ARGS=$(get_config_args_length)
50+
local count_config_args=0
51+
count_config_args=$(get_config_args_length)
5652

57-
for ((j=0;j<COUNT_ARGS;j++)) do
53+
for ((j=0;j<count_config_args;j++)) do
5854

59-
local ARG_OPTIONS=""
60-
ARG_OPTIONS=$(get_config_arg $j text_string)
55+
local arg_options=""
56+
arg_options=$(get_config_arg "$j" text_string)
6157

62-
case $1 in
63-
$ARG_OPTIONS)
64-
echo "${ARG_VALUE[$j]}"
65-
return
66-
;;
67-
esac
58+
if echo "$1" | grep -Eq "$arg_options"; then
59+
echo "${ARG_VALUE[$j]}"
60+
return
61+
fi
6862

6963
done
7064

@@ -77,25 +71,24 @@ function scan_for_args {
7771

7872
while [[ $# -gt 0 ]]
7973
do
80-
local COUNT_ARGS=0
81-
COUNT_ARGS=$(get_config_args_length)
82-
83-
for ((j=0;j<COUNT_ARGS;j++)) do
84-
local ARG_OPTIONS=""
85-
ARG_OPTIONS='+('$(get_config_arg $j short_form)'|'$(get_config_arg $j long_form)')'
86-
87-
case $1 in
88-
$ARG_OPTIONS)
89-
ARG_VALUE[${j}]="$2"
90-
shift # skip argument
91-
;;
92-
*)
93-
;;
94-
esac
74+
75+
local count_config_args=0
76+
count_config_args=$(get_config_args_length)
77+
78+
for ((j=0;j<count_config_args;j++)) do
79+
80+
local arg_options=""
81+
arg_options='('$(get_config_arg "$j" short_form)'|'$(get_config_arg "$j" long_form)')'
82+
83+
if echo "$1" | grep -Eq "$arg_options"; then
84+
ARG_VALUE[${j}]="$2"
85+
shift # skip to next argument
86+
break
87+
fi
9588

9689
done
9790

98-
shift # skip argument
91+
shift # skip to next argument
9992
done
10093

10194
}
@@ -105,21 +98,21 @@ function scan_for_args {
10598
#
10699
function check_for_args_completeness {
107100

108-
local COUNT_ERRS=0
109-
local COUNT_ARGS=0
110-
COUNT_ARGS=$(get_config_args_length)
101+
local count_errs=0
102+
local count_config_args=0
103+
count_config_args=$(get_config_args_length)
111104

112-
for ((j=0;j<COUNT_ARGS;j++)) do
105+
for ((j=0;j<count_config_args;j++)) do
113106

114-
if [ -z "${ARG_VALUE[${j}]}" ] && [ "$(get_config_arg $j required)" == "true" ]; then
115-
printf "%s\n" "Error: $(get_config_arg $j text_string) argument ($(get_config_arg $j short_form)|$(get_config_arg $j long_form)) missing."
116-
((COUNT_ERRS++))
107+
if [ -z "${ARG_VALUE[${j}]}" ] && [ "$(get_config_arg "$j" required)" == "true" ]; then
108+
printf "%s\n" "Error: $(get_config_arg "$j" text_string) argument ($(get_config_arg "$j" short_form)|$(get_config_arg "$j" long_form)) missing."
109+
((count_errs++))
117110
fi
118111

119112
done
120113

121-
if [ ${COUNT_ERRS} -gt 0 ]; then
114+
if [ "${count_errs}" -gt 0 ]; then
122115
quit 1
123116
fi
124117

125-
}
118+
}

general

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
44
# -----------------------------------------------------------------------------
55
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
66
#
7-
# This program is free software: you can redistribute it and/or modify
8-
# it under the terms of the GNU General Public License as published by
9-
# the Free Software Foundation, either version 3 of the License, or
10-
# (at your option) any later version.
7+
# This program is free software: you can redistribute it and/or modify it under
8+
# the terms of the GNU General Public License as published by the Free Software
9+
# Foundation, either version 3 of the License, or (at your option) any later
10+
# version.
11+
#
12+
# This program is distributed in the hope that it will be useful, but WITHOUT
13+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
15+
# <http://www.gnu.org/licenses/> for more details.
1116
#
12-
# This program is distributed in the hope that it will be useful,
13-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15-
# GNU General Public License at <http://www.gnu.org/licenses/> for
16-
# more details.
1717
# -----------------------------------------------------------------------------
1818
#
1919
# bash library for general-purpose script processing
@@ -24,11 +24,11 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2424
#
2525
function check_program_dependencies {
2626

27-
local -a DEPS=("$@")
27+
local -a deps=("$@")
2828

29-
for i in "${DEPS[@]}"; do
29+
for i in "${deps[@]}"; do
3030

31-
if ! type "$i" &>/dev/null; then
31+
if ! command -v "$i" &>/dev/null; then
3232
printf "\n%s\n" "Error: program $i not installed."
3333
quit 1
3434
fi
@@ -42,9 +42,9 @@ function check_program_dependencies {
4242
#
4343
function check_file_dependencies {
4444

45-
local -a DEPS=("$@")
45+
local -a deps=("$@")
4646

47-
for i in "${DEPS[@]}"; do
47+
for i in "${deps[@]}"; do
4848

4949
if [ ! -f "$i" ]; then
5050
printf "\n%s\n" "Error: file $i not found."
@@ -69,35 +69,35 @@ function display_banner {
6969
printf "%s\n" " | $(get_config_details syntax)"
7070
printf "%s\n" " |"
7171

72-
local MAX_COL=0
73-
local COUNT_ARGS=0
74-
COUNT_ARGS=$(get_config_args_length)
72+
local max_col=0
73+
local count_args=0
74+
count_args=$(get_config_args_length)
7575

7676
# determine maximum column width for setting output columns
7777
#
78-
for ((j=0;j<COUNT_ARGS;j++)) do
79-
ARG_LEN=$(get_config_arg $j short_form)+$(get_config_arg $j long_form)
80-
((${#ARG_LEN} > MAX_COL)) && MAX_COL=${#ARG_LEN}
81-
((j == COUNT_ARGS-1 )) && ((MAX_COL+=6))
78+
for ((j=0;j<count_args;j++)) do
79+
arg_len=$(get_config_arg "$j" short_form)+$(get_config_arg "$j" long_form)
80+
((${#arg_len} > max_col)) && max_col=${#arg_len}
81+
((j == count_args-1 )) && ((max_col+=6))
8282
done
8383

84-
for ((j=0;j<COUNT_ARGS;j++)) do
85-
printf "%-${MAX_COL}s %s\n" " | $(get_config_arg $j short_form), $(get_config_arg $j long_form)" "$(get_config_arg $j description)"
84+
for ((j=0;j<count_args;j++)) do
85+
printf "%-${max_col}s %s\n" " | $(get_config_arg "$j" short_form), $(get_config_arg "$j" long_form)" "$(get_config_arg "$j" description)"
8686
done
8787

8888
printf "%s\n\n" " |"
8989

9090
}
9191

9292
# -----------------------------------------------------------------------------
93-
# exit wrapper
93+
# exit program (with exit status passed in)
9494
#
9595
function quit {
9696

97-
local RES=""
98-
RES=("${1}")
97+
local result=""
98+
result=("${1}")
9999

100100
printf "\n"
101-
exit "${RES[0]}"
101+
exit "${result[0]}"
102102

103-
}
103+
}

0 commit comments

Comments
 (0)