Skip to content

Instantly share code, notes, and snippets.

@sempervent
Last active October 12, 2021 23:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sempervent/4d94593e0d56f8fc1b43f92b9983d61f to your computer and use it in GitHub Desktop.
Save sempervent/4d94593e0d56f8fc1b43f92b9983d61f to your computer and use it in GitHub Desktop.
A skeleton for bash Scripts
#!/usr/bin/env bash
# ex: set fdm=marker
# usage {{{1
#/ Usage:
#/ -h|-?|--help)
#/ show this help and exit
#/
# 1}}}
# environment {{{1
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT=${PROJECT:-"Some Project Name in $DIR"}
# 1}}}
# functions {{{1
banner() { # {{{2
# make a static banner with embeded color codes
# BANNER=$(cat <<EOF\n EOF;
# for a simple banner use
# BANNER="The \n$PROJECT\n\t script"
# or have a little funn
# BANNER=$(figlet "$PROJECT" | cowsay)
# or do some coloring
BANNER="\\e[32$PROJECT\\e[39m"
echo -e "$BANNER"
} # 2}}}
die() { # {{{2
echo -e "\\e[31mFAILURE:\\e[39m $1"
exit 1
} # 2}}}
warn() { # {{{2
echo -e "\\e[33mWARNING:\\e[39m $1"
} # 2}}}
info() { # {{{2
echo -e "\\e[32mINFO:\\e[39m $1"
} # 2}}}
show_help() { # {{{2
grep '^#/' "${BASH_SOURCE[0]}" | cut -c4- || \
die "Failed to display usage information"
} # 2}}}
# 1}}}
# arguments {{{1
while :; do
case $1 in # check arguments {{{2
-h|-\?|--help) # help {{{3
banner
show_help
exit
;; # 3}}}
-?*) # unknown argument {{{3
warn "Unknown option (ignored): $1"
shift
;; # 3}}}
*) # default {{{3
break # 3}}}
esac # 2}}}
done
# 1}}}
# logic {{{1
banner
# 1}}}
@sempervent
Copy link
Author

sempervent commented Sep 23, 2019

I use the following in my .bashrc to easily generate this skeleton to a file for quick deployment of a bash script:

make_bash_script() {
wget -O "$1" https://gist.githubusercontent.com/sempervent/4d94593e0d56f8fc1b43f92b9983d61f/raw/f4d761ad28ec20ceb45c4ae03f32628bb868946e/bash_skeleton.sh
}

usage is make_bash_script <name of script>

@Kardelio
Copy link

Love the idea of this and infact the script itself its just a little difficult to read with all the hyphens, otherwise very cool!
Also a fan of your make_bash_scripts function in your bashrc very clever

@sempervent
Copy link
Author

sempervent commented Feb 6, 2020

I've updated the script with the capabilities of passing an ENV var $PROJECT, with the below function added to your vimrc, it can be set within your env or passed when you invoke your new script.

I've also revised my make_bash_script function to chmod +x and then vim the file you created

make_bash_script() {
SKELETON_URL=${BASH_SKELETON_URL:-"https://gist.githubusercontent.com/sempervent/4d94593e0d56f8fc1b43f92b9983d61f/raw/fe08f3de2b023dee323914ad564a5d746f4c6348/bash_skeleton.sh"}
wget -O "$1" "$SKELETON_URL"
chmod +x "$1"
vim "$1"
}

usage is

# this will make a bash script called "test.sh"
# this makes the script executable and opens the script in vim
make_bash_script test.sh
# if your project requires multiple scripts, you can EXPORT PROJECT or use project environment management
PROJECT="my silly project" test.sh >> more_specific_script.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment