Last active
September 13, 2021 20:00
-
-
Save cjmcgraw/d0447a38289bae9de9c7eaf07f3334b0 to your computer and use it in GitHub Desktop.
This is a custom way that I parse args in bash scripts. I consider it the most terse and readable form.
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
#! /usr/bin/env bash | |
first="" | |
second="" | |
third_flag="" | |
function usage() { | |
echo "$0 args" | |
echo "" | |
echo "required arguments:" | |
echo " --first first argument" | |
echo " --second second argument" | |
} | |
while [[ $# -gt 0 ]];do case $1 in | |
--first) first="$2"; shift;; | |
--second) second="$2"; shift ;; | |
--third) third_flag="1";; | |
--help) usage; exit 0 ;; | |
esac; shift; done | |
if [[ -z "$first" ]] || [[ -z "$second" ]]; then | |
echo "missing required arguments" | |
usage | |
exit 1 | |
fi | |
echo "third_flag=${third_flag}"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its not exactly the best. But I hate all the other ways of doing it. If you have more than a few parameters.. you probably should be using python.