Skip to content

Instantly share code, notes, and snippets.

@chinchalinchin
Last active October 21, 2023 05:18
Show Gist options
  • Save chinchalinchin/576031ffd88e1b628bf19cabe149f1c9 to your computer and use it in GitHub Desktop.
Save chinchalinchin/576031ffd88e1b628bf19cabe149f1c9 to your computer and use it in GitHub Desktop.
BASH script to parse command line arguments
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
SCRIPT_NAME="arg-parse"
SCRIPT_DES=$'pass in arguments with \e[3m./scripts/arg-parse --arg1 this --arg2 that\e[0m'
function log(){
echo -e "\e[92m$(date +"%r")\e[0m: \e[4;32m$SCRIPT_NAME\e[0m : >> $1"
}
function help(){
echo -e "\n\e[4m$SCRIPT_NAME\e[0m\n\n\t$SCRIPT_DES"
}
while [[ $# -gt 0 ]]; do
case $1 in
--arg1)
ARG1="$2"
shift
shift
;;
--arg2)
ARG2="$2"
shift
shift
;;
--help)
help
exit 0
;;
*)
log "Input not understood. See \e[3m--help\e[0m for information on using this command."
exit 1
;;
esac
done
log "first arg: $ARG1"
log "second arg: $ARG2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment