-
-
Save gabrie30/fbbd6c2b6f1bb9bf650d5374b415f0fa to your computer and use it in GitHub Desktop.
A skeleton for bash Scripts
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 | |
# usage {{{1 ------------------------------------------------------------------ | |
#/ Usage: | |
#/ -h|-?|--help) | |
#/ show this help and exit | |
#/ | |
# 1}}} ------------------------------------------------------------------------ | |
# environment {{{1 ------------------------------------------------------------ | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
# 1}}} ------------------------------------------------------------------------ | |
# functions {{{1 -------------------------------------------------------------- | |
banner() { # {{{2 ------------------------------------------------------------- | |
echo -e "\\e[31m Some Project Banner\\e[39m" | |
} # 2}}} ---------------------------------------------------------------------- | |
die() { # {{{2 ---------------------------------------------------------------- | |
echo -e "\\e[31mFAILURE:\\e[39m $1" | |
exit 1 | |
} # 2}}} ---------------------------------------------------------------------- | |
warn() { # {{{2 --------------------------------------------------------------- | |
echo -e "\\e[33mWARNING:\\e[39m $1" | |
} # 2}}} ---------------------------------------------------------------------- | |
show_help() { # {{{2 ---------------------------------------------------------- | |
grep '^#/' "${BASH_SOURCE[0]}" | cut -c4- || \ | |
die "Failed to display usage information" | |
} # 2}}} ---------------------------------------------------------------------- | |
# 1}}} ------------------------------------------------------------------------ | |
# arguments {{{1 -------------------------------------------------------------- | |
while :; do | |
case $1 in # check arguments {{{2 ------------------------------------------- | |
-h|-\?|--help) # help {{{3 ------------------------------------------------ | |
banner | |
show_help | |
exit | |
;; # 3}}} --------------------------------------------------------------- | |
-?*) # unknown argument {{{3 ---------------------------------------------- | |
warn "Unknown option (ignored): $1" | |
shift | |
;; # 3}}} --------------------------------------------------------------- | |
*) # default {{{3 --------------------------------------------------------- | |
break # 3}}} ------------------------------------------------------------ | |
esac # 2}}} ----------------------------------------------------------------- | |
done | |
# 1}}} ------------------------------------------------------------------------ | |
# logic {{{1 ------------------------------------------------------------------ | |
banner | |
# 1}}} ------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment