Skip to content

Instantly share code, notes, and snippets.

@djmott
Last active November 24, 2017 02:08
Show Gist options
  • Save djmott/3fdb903b238099bb00a58f4a21d5d750 to your computer and use it in GitHub Desktop.
Save djmott/3fdb903b238099bb00a58f4a21d5d750 to your computer and use it in GitHub Desktop.
Skeleton shell script
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
main(){
export _K="$(tput setaf 0)"
export _R="$(tput setaf 1)"
export _G="$(tput setaf 2)"
export _Y="$(tput setaf 3)"
export _B="$(tput setaf 4)"
export _M="$(tput setaf 5)"
export _C="$(tput setaf 6)"
export _W="$(tput setaf 7)"
export _Z="$(tput sgr0)"
export _BOLD="$(tput bold)"
export _BLINK="$(tput blink)"
export _REVERSE="$(tput smso)"
export _UNDERLINE="$(tput smul)"
export SCRIPTDIR="$( dirname "$( readlink -f "${BASH_SOURCE[0]}" )")"
export RUNOUT=${TMP:/tmp}/.$$.run
trap .at.exit INT TERM EXIT
$@
return 0
}
.at.exit(){
local _ret=$?
[[ -f $RUNOUT ]] && cat $RUNOUT
[[ "0" != "$_ret" ]] && echo -e "$_R Exiting with code: $_ret $_Z"
rm -f $RUNOUT
exit $_ret
}
.run(){
local _msg="$1"
shift
echo -e "$_R COMMAND FAILED $_Z" > $RUNOUT
echo -e "$_Y $@ $_Z" >> $RUNOUT
echo "" >> $RUNOUT
($@) >> $RUNOUT 2>&1 &
local _pid=$!
local _spin='-\|/'
local i=0
while kill -0 $_pid 2>/dev/null; do
i=$(( (i+1) %4 ))
printf "\r$_Y $_msg ${_spin:$i:1} $_Z"
sleep .1
done
wait $_pid
rm $RUNOUT
echo ""
}
main $@
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment