Boilerplate for bash cli with arguesments.
Last active
November 29, 2020 00:50
-
-
Save noinarisak/d8da0a0c7818665bc0186ec3aef36f6b to your computer and use it in GitHub Desktop.
Bash Shell Boilerplate
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
| #!/usr/bin/env bash | |
| set -o errexit | |
| set -o pipefail | |
| set -o nounset | |
| # set -o xtrace | |
| ##### Magic Variables | |
| # Set magic variables for current file & dir | |
| __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" | |
| __base="$(basename ${__file} .sh)" | |
| __root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app | |
| arg1="${1:-}" | |
| printf "${__dir}\n" | |
| printf "${__file}\n" | |
| printf "${__base}\n" | |
| printf "${__root}\n" | |
| ##### Functions | |
| usage() | |
| { | |
| echo "usage: bash-shell-boilerplate.sh [[[-f file ] [-i]] | [-h]]" | |
| } | |
| ##### Main | |
| while [ "$1" != "" ]; do | |
| case $1 in | |
| -f | --file ) shift | |
| filename=$1 | |
| ;; | |
| -i | --interactive ) interactive=1 | |
| ;; | |
| -h | --help ) usage | |
| exit | |
| ;; | |
| * ) usage | |
| exit 1 | |
| esac | |
| shift | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment