Skip to content

Instantly share code, notes, and snippets.

@noinarisak
Last active November 29, 2020 00:50
Show Gist options
  • Save noinarisak/d8da0a0c7818665bc0186ec3aef36f6b to your computer and use it in GitHub Desktop.
Save noinarisak/d8da0a0c7818665bc0186ec3aef36f6b to your computer and use it in GitHub Desktop.
Bash Shell Boilerplate

Bash Shell Boilerplate

Boilerplate for bash cli with arguesments.

#!/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