#===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
+must() { "$@" || die "cannot $*"; } #o
vbm() {
# Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true".
# Usage: vbm "DEBUG :verbose message here"
processArgs() {
# Desc: Processes arguments provided to script.
# Usage: processArgs "$@"
- # Version: 1.0.0
+ # Version: 1.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")
# arrayPosArgs Array of remaining positional argments
# Depends:
# yell() Displays messages to stderr.
+ # die() Displays message to stderr then exits with error code.
# vbm() Displays messsages to stderr if opVerbose set to "true".
# showUsage() Displays usage information about parent script.
# showVersion() Displays version about parent script.
# [2]: "Handling positional parameters" (2018-05-12). https://wiki.bash-hackers.org/scripting/posparams
# Initialize function
- vbm "DEBUG:processArgs function called."
+ vbm "DEBUG:processArgs function called.";
# Perform work
+ if [ $# -le 0 ]; then showUsage; die "FATAL:No arguments provided."; fi;
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].
-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";
+ vbm "DEBUG:Input file pathFileIn1 set to:${pathFileIn1}";
shift;
- vbm "DEBUG:Input file pathFileIn1 set to:""$2";
else
- yell "ERROR: Specified input file does not exist:""$2";
- yell "Exiting.";
- exit 1;
- fi ;;
+ die "FATAL: Specified input file does not exist:$2";
+ 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";
+ vbm "DEBUG:Input directory pathDirIn1 set to:${pathDirIn1}";
+ shift;
else # Display error if $2 is not a valid dir.
- yell "ERROR:Specified input directory does not exist:""$2";
- yell "Exiting.";
- exit 1;
- fi ;;
+ die "FATAL:Specified input directory does not exist:$2";
+ 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;;
+ *) die "FATAL:Invalid selection.";;
esac
if [ "$opFileOut1_overwrite" == "true" ]; then
pathFileOut1="$2";
- shift;
- vbm "DEBUG:Output file pathFileOut1 set to:""$2";
+ vbm "DEBUG:Output file pathFileOut1 set to:${pathFileOut1}";
+ shift;
else
- yell "ERORR:Exiting in order to not overwrite output file:""$pathFileOut1";
- exit 1;
- fi
+ die "FATAL:Exiting in order to not overwrite output file:${pathFileOut1}";
+ fi;
else
pathFileOut1="$2";
- shift;
- vbm "DEBUG:Output file pathFileOut1 set to:""$2";
- fi ;;
+ vbm "DEBUG:Output file pathFileOut1 set to:${pathFileOut1}";
+ shift;
+ 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";
+ vbm "DEBUG:Output directory pathDirOut1 set to:${pathDirOut1}";
+ shift;
else
- yell "ERROR:Specified output directory is not valid:""$2";
- yell "Exiting.";
- exit 1;
- fi ;;
+ die "FATAL:Specified output directory is not valid:$2";
+ fi;;
--) # End of all options. See [2].
shift;
for arg in "$@"; do
arrayPosArgs+=("$arg");
done;
break;;
- -*) showUsage; yell "ERROR: Unrecognized option."; exit 1;; # Display usage
- *) showUsage; yell "ERROR: Unrecognized argument."; exit 1;; # Handle unrecognized options. See [1].
- esac
- shift
- done
+ -*) showUsage; die "FATAL: Unrecognized option.";; # Display usage
+ *) showUsage; die "FATAL: Unrecognized argument.";; # Handle unrecognized options. See [1].
+ esac;
+ shift;
+ done;
# End function
- vbm "DEBUG:processArgs function ended."
+ vbm "DEBUG:processArgs function ended.";
return 0; # Function finished.
} # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).