Skip to content

Instantly share code, notes, and snippets.

@cjmcgraw
Last active September 13, 2021 20:00
Show Gist options
  • Save cjmcgraw/d0447a38289bae9de9c7eaf07f3334b0 to your computer and use it in GitHub Desktop.
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.
#! /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}";
@cjmcgraw
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment