Last active
October 21, 2023 05:18
-
-
Save chinchalinchin/576031ffd88e1b628bf19cabe149f1c9 to your computer and use it in GitHub Desktop.
BASH script to parse command line arguments
This file contains hidden or 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 | |
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