Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,28 @@

**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:

- `General`: Routines created to check program and file dependencies, banner display, *etc*.
- `Args`: Routines created to manage command-line argument parsing
- `general`: Routines created to:
- Check program dependencies
- Check file dependencies
- Format and display a program banner, e.g.,
>>
|
| A bash template (BaT) to ease argument parsing and management
| 0.2.0
|
| Usage:
| bash_template.sh -a alpha -b bravo [-c charlie] -d delta
|
| -a, --alpha alpha (something descriptive)
| -b, --bravo bravo (something descriptive)
| -c, --charlie charlie (this is optional)
| -d, --delta delta (something descriptive)
|

- `args`: Routines created to:
- Parse a JSON file for program configuration details
- Scan command-line arguments for accuracy and completeness

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.

> 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.
95 changes: 44 additions & 51 deletions args
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# -----------------------------------------------------------------------------
#
# bash library for command line argument loading/parsing/validating
#

EXEC_DIR="$(dirname "$0")"
# shellcheck source=bash-lib/general
source "${EXEC_DIR}/bash-lib/general"

ARGS_FILE="${EXEC_DIR}/data/config.json"
readonly ARGS_FILE="data/config.json"

# -----------------------------------------------------------------------------
# query data/config.json, returning the details object
Expand All @@ -40,7 +36,7 @@ function get_config_arg {
}

# -----------------------------------------------------------------------------
# query data/config.json, returning the arguments object length
# query data/config.json, returning the count of arguments
#
function get_config_args_length {
printf "%s" "$(jq -r '.arguments|length' < "${ARGS_FILE}")"
Expand All @@ -51,20 +47,18 @@ function get_config_args_length {
#
function get_config_arg_value {

local COUNT_ARGS=0
COUNT_ARGS=$(get_config_args_length)
local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<COUNT_ARGS;j++)) do
for ((j=0;j<count_config_args;j++)) do

local ARG_OPTIONS=""
ARG_OPTIONS=$(get_config_arg $j text_string)
local arg_options=""
arg_options=$(get_config_arg "$j" text_string)

case $1 in
$ARG_OPTIONS)
echo "${ARG_VALUE[$j]}"
return
;;
esac
if echo "$1" | grep -Eq "$arg_options"; then
echo "${ARG_VALUE[$j]}"
return
fi

done

Expand All @@ -77,25 +71,24 @@ function scan_for_args {

while [[ $# -gt 0 ]]
do
local COUNT_ARGS=0
COUNT_ARGS=$(get_config_args_length)

for ((j=0;j<COUNT_ARGS;j++)) do
local ARG_OPTIONS=""
ARG_OPTIONS='+('$(get_config_arg $j short_form)'|'$(get_config_arg $j long_form)')'

case $1 in
$ARG_OPTIONS)
ARG_VALUE[${j}]="$2"
shift # skip argument
;;
*)
;;
esac

local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<count_config_args;j++)) do

local arg_options=""
arg_options='('$(get_config_arg "$j" short_form)'|'$(get_config_arg "$j" long_form)')'

if echo "$1" | grep -Eq "$arg_options"; then
ARG_VALUE[${j}]="$2"
shift # skip to next argument
break
fi

done

shift # skip argument
shift # skip to next argument
done

}
Expand All @@ -105,21 +98,21 @@ function scan_for_args {
#
function check_for_args_completeness {

local COUNT_ERRS=0
local COUNT_ARGS=0
COUNT_ARGS=$(get_config_args_length)
local count_errs=0
local count_config_args=0
count_config_args=$(get_config_args_length)

for ((j=0;j<COUNT_ARGS;j++)) do
for ((j=0;j<count_config_args;j++)) do

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

done

if [ ${COUNT_ERRS} -gt 0 ]; then
if [ "${count_errs}" -gt 0 ]; then
quit 1
fi

}
}
56 changes: 28 additions & 28 deletions general
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License at
# <http://www.gnu.org/licenses/> for more details.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# -----------------------------------------------------------------------------
#
# bash library for general-purpose script processing
Expand All @@ -24,11 +24,11 @@ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#
function check_program_dependencies {

local -a DEPS=("$@")
local -a deps=("$@")

for i in "${DEPS[@]}"; do
for i in "${deps[@]}"; do

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

local -a DEPS=("$@")
local -a deps=("$@")

for i in "${DEPS[@]}"; do
for i in "${deps[@]}"; do

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

local MAX_COL=0
local COUNT_ARGS=0
COUNT_ARGS=$(get_config_args_length)
local max_col=0
local count_args=0
count_args=$(get_config_args_length)

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

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

printf "%s\n\n" " |"

}

# -----------------------------------------------------------------------------
# exit wrapper
# exit program (with exit status passed in)
#
function quit {

local RES=""
RES=("${1}")
local result=""
result=("${1}")

printf "\n"
exit "${RES[0]}"
exit "${result[0]}"

}
}