Code Monkey home page Code Monkey logo

bashtemplate's Introduction

Bash template

Use this bash-template for your next script! This will give you cleaner output, better error handling and more features. This script is based on the rules of Googles Styleguide and bahamas10's Styguide

Output example:

There are the following output classes: info, warn, success, debug and error. The classes are structured as follows (info function as example):

info() {
  local _date
  _date=$(date +%d-%H.%M)
  echo -e "[$_date][INFO]: $1 "
} 

So you can easily filter the output your script produces. Example usage:

info " I am some random info."
warn "This is not normal!"
succ "Finished without errors!"
debug "I'm some info just for the devs!"
err "Error, something is wrong!"
err_die "Error, exiting now!"

In addition, some functions have more features than just clean output.

Debug function

The debug() function will only show up if boolean 'is_debug' is true. This is made by the -d parameter. So devs can just execute the script with -d to view all debug messages.

$ bash /usr/local/bin/script.sh -d

Err-Die function

The err_die() function redirects the message to STDERR, starts the cleanup function and then exits. You can call err_die() with "1" as argument to show the help before exiting.

err_die "Please use parameter." 1

You can call normal errors without exiting the script by just

err "This is an error but I can continue anyway."

Traps

The following traps are builtin:

trap exit_EXIT EXIT
trap exit_CTRL QUIT SIGINT

The trap function ensures that the script recognizes if it is aborted by the user (ctrl+c), or if it crashes due to other reasons. As soon as it detects an abort, the cleanup function is started, thus ensuring that the cleanup function also runs in case of unplanned aborts.

This function is ALWAYS started when the script ends. Therefore the cleanup function is not included in the main function.

exit_EXIT() {
  info "Script ended! Cleanup & Exit."
  cleanup
  exit 1
}

This function is called when the user presses ctrl+c.

exit_CTRL() {
  err "User pressed CTRL+C!"
  exit 1
}

Changing Colors

Just change the color pallet values at the top of the script as you whish.

readonly cf="\\033[0m"
readonly red="\\033[0;31m"
readonly green="\\033[0;32m"
readonly yellow="\\033[0;33m"
readonly purple="\\033[0;35m"

Quick Start:

Just download the script

wget -O mynewscript.sh https://raw.githubusercontent.com/lfkdev/bashtemplate/master/template_script.bash
chmod +x mynewscript.sh

Fill your information into the metabox

####################################################
# Author:                                          #
# Info:                                            #
# License:                                         #
####################################################

Now start creating your code with the output functions and write every function in the main one! Have fun

If you have any problems or whishes let me now.

bashtemplate's People

Contributors

lfkdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.