From: Steven Baltakatei Sandoval Date: Tue, 13 Jul 2021 22:12:38 +0000 (+0000) Subject: feat(unitproc):Add bktemp-processArgs X-Git-Tag: 0.4.7 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/792d052654b0b5e5f8973cfc698bb72145c8b738 feat(unitproc):Add bktemp-processArgs - feat(unitproc):Add bktemp-showUsage as symlink to bktemp-processArgs - feat(unitproc):Add bktemp-showVersion as symlink to bktemp-processArgs - chore(unitproc):Minor comment formatting of bktemp-checkAppFileDir - feat(vbm):Change bktemp-vbm to use `opVerbose` instead of `optionVerbose` --- diff --git a/unitproc/bktemp-checkAppFileDir b/unitproc/bktemp-checkAppFileDir index e21efb9..d5a620b 100644 --- a/unitproc/bktemp-checkAppFileDir +++ b/unitproc/bktemp-checkAppFileDir @@ -2,10 +2,11 @@ # Desc: Checks that apps, files, or dirs exist. #==BEGIN Define script parameters== +#===BEGIN Define variables=== declare -Ag appRollCall # Associative array for storing app status declare -Ag fileRollCall # Associative array for storing file status declare -Ag dirRollCall # Associative array for storing dir status -#==END Define script parameters== +#===END Define variables=== #===BEGIN Declare local script functions=== checkapp() { diff --git a/unitproc/bktemp-processArgs b/unitproc/bktemp-processArgs new file mode 100644 index 0000000..ca29c35 --- /dev/null +++ b/unitproc/bktemp-processArgs @@ -0,0 +1,198 @@ +#!/bin/bash +# Desc: Processes script arguments + +#==BEGIN Define script parameters== +#===BEGIN Define variables=== +: +#===END Define variables=== + +#===BEGIN Declare local script functions=== +yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique +die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 +try() { "$@" || die "cannot $*"; } #o +vbm() { + # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true". + # Usage: vbm "DEBUG :verbose message here" + # Version 0.2.0 + # Input: arg1: string + # vars: opVerbose + # Output: stderr + # Depends: bash 5.0.3, GNU-coreutils 8.30 (echo, date) + + if [ "$opVerbose" = "true" ]; then + functionTime="$(date --iso-8601=ns)"; # Save current time in nano seconds. + echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text. + fi + + # End function + return 0; # Function finished. +} # Displays message if opVerbose true +showUsage() { + # Desc: Display script usage information + # Usage: showUsage + # Version 0.0.1 + # Input: none + # Output: stdout + # Depends: GNU-coreutils 8.30 (cat) + cat <<'EOF' + USAGE: + bktemp-processArgs [ options ] [FILE...] + + OPTIONS: + -h, --help + Display help information. + --version + Display script version. + -v, --verbose + Display debugging info. + -i, --input-file + Define input file path. + -I, --input-dir + Define input directory path. + -o, --output-file + Define output file path + -O, --output-dir + Define output directory path + + EXAMPLE: + bktemp-processArgs -o foo.txt +EOF +} # Display information on how to use this script. +showVersion() { + # Desc: Displays script version and license information. + # Usage: showVersion + # Version: 0.0.1 + # Input: scriptVersion var containing version string + # Output: stdout + # Depends: vbm(), yell, GNU-coreutils 8.30 + + # Initialize function + vbm "DEBUG:showVersion function called." + + cat <<'EOF' +bktemp-processArgs 0.0.1 +Copyright (C) 2021 Steven Baltakatei Sandoval +License GPLv3: GNU GPL version 3 +This is free software; you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + + GNU Coreutils 8.30 + Copyright (C) 2018 Free Software Foundation, Inc. + License GPLv3+: GNU GPL version 3 or later . + This is free software: you are free to change and redistribute it. + There is NO WARRANTY, to the extent permitted by law. +EOF + + # End function + vbm "DEBUG:showVersion function ended." + return 0; # Function finished. +} # Display script version. +processArgs() { + # Desc: Processes arguments provided to script. + # Usage: processArgs "$@" + # Version: 0.0.1 + # Input: "$@" (list of arguments provided to the function) + # Output: Sets following variables used by other functions: + # opVerbose Indicates verbose mode enable status. (ex: "true", "false") + # pathDirOut1 Path to output directory. + # pathFileOut1 Path to output file. + # pathDirIn1 Path to input directory. + # pathFileIn1 Path to input file. + # opFileOut1_overwrite Indicates whether file pathFileOut1 should be overwritten (ex: "true", "false') + # Depends: + # yell() Displays messages to stderr. + # vbm() Displays messsages to stderr if opVerbose set to "true". + # showUsage() Displays usage information about parent script + # showVersion() Displays version about parent script + # External dependencies: bash (5.0.3), echo + # Ref./Attrib.: + # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347 + + # Initialize function + vbm "DEBUG:processArgs function called." + + # Perform work + while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0). + #yell "DEBUG:Starting processArgs while loop." # Debug stderr message. See [1]. + #yell "DEBUG:Provided arguments are:""$*" # Debug stderr message. See [1]. + case "$1" in + -h | --help) showUsage; exit 1;; # Display usage. + --version) showVersion; exit 1;; # Show version + -v | --verbose) opVerbose="true"; vbm "DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1]. + -i | --input-file) # Define input file path + if [ -f "$2" ]; then # If $2 is file that exists, set pathFileIn1 to $2, pop $2. + pathFileIn1="$2"; + shift; + vbm "DEBUG:Input file pathFileIn1 set to:""$2"; + else + yell "ERROR: Specified input file does not exist:""$2"; + yell "Exiting."; + exit 1; + fi ;; + -I | --input-dir) # Define input directory path + if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirIn1 to $2, pop $2. + pathDirIn1="$2"; + shift; + vbm "DEBUG:Input directory pathDirIn1 set to:""$2"; + else # Display error if $2 is not a valid dir. + yell "ERROR:Specified input directory does not exist:""$2"; + yell "Exiting."; + exit 1; + fi ;; + -o | --output-file) # Define output file path + if [ -f "$2" ]; then # If $2 is file that exists, prompt user to continue to overwrite, set pathFileOut1 to $2, pop $2. + yell "Specified output file $2 already exists. Overwrite? (y/n):" + read -r m; case $m in + y | Y | yes) opFileOut1_overwrite="true";; + n | N | no) opFileOut1_overwrite="false";; + *) yell "Invalid selection. Exiting."; exit 1;; + esac + if [ "$opFileOut1_overwrite" == "true" ]; then + pathFileOut1="$2"; + shift; + vbm "DEBUG:Output file pathFileOut1 set to:""$2"; + else + yell "ERORR:Exiting in order to not overwrite output file:""$pathFileOut1"; + exit 1; + fi + else + pathFileOut1="$2"; + shift; + vbm "DEBUG:Output file pathFileOut1 set to:""$2"; + fi ;; + -O | --output-dir) # Define output directory path + if [ -d "$2" ]; then # If $2 is dir that exists, set pathDirOut1 to $2, pop $2 + pathDirOut1="$2"; + shift; + vbm "DEBUG:Output directory pathDirOut1 set to:""$2"; + else + yell "ERROR:Specified output directory is not valid:""$2"; + yell "Exiting."; + exit 1; + fi ;; + *) yell "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1]. + esac + shift + done + + # End function + vbm "DEBUG:processArgs function ended." + return 0; # Function finished. +} # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.). + +#===END Declare local script functions=== +#==END Define script parameters== + +#==BEGIN sample code== +processArgs "$@"; +yell "DEBUG:Provided arguments..:""$*" +yell "DEBUG:opVerbose...........:$opVerbose"; +yell "DEBUG:pathDirOut1.........:$pathDirOut1"; +yell "DEBUG:pathFileOut1........:$pathFileOut1"; +yell "DEBUG:pathDirIn1..........:$pathDirIn1"; +yell "DEBUG:pathFileIn1.........:$pathFileIn1"; +yell "DEBUG:opFileOut1_overwrite:$opFileOut1_overwrite"; +#==END sample code== + +# Author: Steven Baltaktei Sandoval +# License: GPLv3+ diff --git a/unitproc/bktemp-showUsage b/unitproc/bktemp-showUsage new file mode 120000 index 0000000..542b1f1 --- /dev/null +++ b/unitproc/bktemp-showUsage @@ -0,0 +1 @@ +bktemp-processArgs \ No newline at end of file diff --git a/unitproc/bktemp-showVersion b/unitproc/bktemp-showVersion new file mode 120000 index 0000000..542b1f1 --- /dev/null +++ b/unitproc/bktemp-showVersion @@ -0,0 +1 @@ +bktemp-processArgs \ No newline at end of file diff --git a/unitproc/bktemp-vbm b/unitproc/bktemp-vbm index 8fa7c1c..878a022 100644 --- a/unitproc/bktemp-vbm +++ b/unitproc/bktemp-vbm @@ -1,27 +1,27 @@ #!/bin/bash -# Desc: Display message if optionVerbose true +# Desc: Display message if opVerbose true vbm() { - # Description: Prints verbose message ("vbm") to stderr if optionVerbose is set to "true". + # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true". # Usage: vbm "DEBUG :verbose message here" - # Version 0.1.3 + # Version 0.2.0 # Input: arg1: string - # vars: optionVerbose + # vars: opVerbose # Output: stderr # Depends: bash 5.0.3, echo 8.30, date 8.30 - if [ "$optionVerbose" = "true" ]; then - functionTime=$(date --iso-8601=ns); # Save current time in nano seconds. - echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text. + if [ "$opVerbose" = "true" ]; then + functionTime="$(date --iso-8601=ns)"; # Save current time in nano seconds. + echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text. fi # End function return 0; # Function finished. -} # Displays message if optionVerbose true +} # Displays message if opVerbose true #==BEGIN sample code== vbm "STATUS:This is a status message."; -optionVerbose="true"; +opVerbose="true"; vbm "STATUS:This is another status message."; #==END sample code==