Skip to content

Instantly share code, notes, and snippets.

@lalyos
Last active October 13, 2023 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lalyos/86e3edeec74377846090 to your computer and use it in GitHub Desktop.
Save lalyos/86e3edeec74377846090 to your computer and use it in GitHub Desktop.
bash script skeleton following https://github.com/progrium/bashstyle

To start a new script use this one-liner:

curl -Lo newscript.sh http://j.mp/bash-skeleton
#!/usr/bin/env bash
debug() {
if ((DEBUG)); then
echo "===> [${FUNCNAME[1]}] $*" 1>&2
fi
}
list-commands() {
echo "available commands:"
sed -n 's/\(.*\)().*/ ::\1/p' ${BASH_SOURCE} \
| grep -v 'main\|debug\|sed\|list-commands' \
| sort
}
main() {
# if last arg is -d sets DEBUG
[[ ${@:$#} == -d ]] && { set -- "${@:1:$(($#-1))}" ; DEBUG=1 ; } || :
if [[ $1 =~ :: ]]; then
debug DIRECT-COMMAND ...
command=${1#::}
shift
$command "$@"
else
debug default-command
list-commands
fi
}
alias r=". $BASH_SOURCE"
[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true
#!/bin/bash
set -eo pipefail
if [[ "$TRACE" ]]; then
: ${START_TIME:=$(date +%s)}
export START_TIME
export PS4='+ [TRACE $BASH_SOURCE:$LINENO][ellapsed: $(( $(date +%s) - $START_TIME ))] '
set -x
fi
debug() {
[[ "$DEBUG" ]] && echo "-----> $*" 1>&2
}
main() {
: ${DEBUG:=1}
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment