Skip to content

Instantly share code, notes, and snippets.

@henryiii henryiii/bstemplate.bash
Last active Dec 18, 2015

Embed
What would you like to do?
Bash Script Template, copy useful parts.
#!/usr/bin/env bash
# No argument version (any argument prints help)
if [ $# -gt 0 ] ; then
echo "$(echo $0 | sed "s;$HOME;~;")"
echo "Help command output"
exit 0
fi
# Sudo-only script with no arguemnts
if [ $# -gt 0 ] || [[ $UID != 0 ]] ; then
if [[ $UID != 0 ]] && [ $# -eq 0 ]; then
echo "Usage: sudo $(echo $0 | sed "s;$HOME;~;")"
echo "Help message: $(echo $0 | sed "s;$HOME;~;") -h"
echo
echo "Run with sudo to ..."
else
echo "sudo $(echo $0 | sed "s;$HOME;~;")"
echo "Help command output"
fi
if [[ $UID != 0 ]] ; then exit 1; else exit 0; fi
fi
# Standard bash stuff to get the current directory
unset CDPATH
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
cd $DIR
# ColorStuff
txtrst=$(tput sgr0)
txtbold=$(tput bold)
# Use $(tput setaf 1), etc to set color
# Color loop
var=1
maxvar=8
for i in ...; do
echo -n "$(tput setaf $var)"
...
echo "$txtrst"
[ $var = 6 ] && var=8
var=$(((var+1)%maxvar))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.