Last active
June 4, 2022 21:18
-
-
Save halvener/6178768 to your computer and use it in GitHub Desktop.
Bash Template - Just a simple bash template that allows for easy argument parsing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
shopt -s -o nounset | |
################################################### | |
# File: | |
# Notes: | |
# Revisions: | |
# Date Developer Description | |
# ---------- --------- ----------- | |
# YYYY-MM-DD Name Initialized file | |
################################################### | |
################################################### | |
# Initialize Variables | |
INPUT_VALUES="" | |
VERBOSITY=0 | |
################################################### | |
# Functions | |
################################################### | |
# Parse Arguments | |
PREV_ARG="" | |
while [ $# -gt 0 ]; do | |
# Code to allow for multiple actions for an agrument | |
if [ -n "$PREV_ARG" ] && [ $(echo $1 | grep "^-" -c) -eq 0 ] ; then | |
case "$PREV_ARG" in | |
-opt1) INPUT_VALUES="$INPUT_VALUES $1" | |
[ $VERBOSITY -gt 0 ] && echo "Received input value: '$1'" | |
shift | |
continue;; | |
esac | |
fi | |
PREV_ARG="$1" | |
# Parse through the arguments | |
case "$1" in | |
-opt1) INPUT_VALUES="$2" | |
[ $VERBOSITY -gt 0 ] && echo "Received input value: '$2'" | |
shift;; | |
-v|--verbose) VERBOSITY=1 | |
echo "Verbose mode on";; | |
*) echo "ERROR: Could not parse option: '$1', exiting..." | |
exit 1;; | |
esac | |
shift | |
done | |
################################################## | |
# Run program | |
echo "Received input values: ${INPUT_VALUES}" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment