diff --git a/bsf b/bsf index cf4bf26..83cf953 100644 --- a/bsf +++ b/bsf @@ -3,7 +3,7 @@ # Bash Framework # Some useful bash functions to make things easier. # -# Copyright (C) 2017-2018 willipink.eu +# Copyright (C) 2017-2019 willipink.eu # Author Moritz Münch moritzmuench@mailbox.org # # This program is free software: you can redistribute it and/or modify @@ -20,6 +20,10 @@ # along with this program. If not, see . + +# Variables + +# supress colors if used to write to a log LOG=True for arg in $*; do if [[ "$arg" == "--log" ]]; then @@ -28,8 +32,7 @@ for arg in $*; do done -# VARIABLES -## COLORS (use ANSI escape codes) +## colors (use ANSI escape codes) if [[ "$LOG" == True ]]; then normal () { printf '\033[0m' ; } red () { printf '\033[0;31m' ; } @@ -71,22 +74,32 @@ else fi -# FUNCTIONS -# Print somethong to STDOUT. Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html -# @param string what to say + +# Functions say () { + # Print something to stdout. + + # Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html + + # @param what to print + printf %s\\n "$*" return 0 } -# Print to Stdout. Do not finish with a linebreak + sayn () { + # Like say without linebreak. + + # @param what to print + printf %s "$*" return 0 } -# quick styler, usage "style *color*" sayc() { + # quick styler, usage "style *color*" + if [[ "$1" == "" ]]; then blue else @@ -98,6 +111,7 @@ sayc() { normal return 0 } + saycn() { $1 sayn " ::: " @@ -107,24 +121,21 @@ saycn() { return 0 } - -# check if user is root -# @return true if user is root, else false root () { + # Exit immediately if user is not root + # Usage: root + if [[ "$EUID" -ne 0 ]]; then say sayc red "You need to be root." exit 1 - else - say - sayc green "You are root." - return 0 fi } - -# Check if last call had errors. If so, print error message and exit. ok() { + # Check if last call had errors. If so, print error message and exit. + # Usage: ok + if [[ "$?" -ne 0 ]]; then sayc red "ERROR, aborting." exit 1 @@ -134,25 +145,17 @@ ok() { fi } -# Do a job and check if it ran successfull. -# @param $*: command to execute try() { - $* - if [[ "$?" -ne 0 ]]; then - sayc red "ERROR, aborting" - say - exit 1 - else - sayc green "done" - return 0 - fi + # Do a job and check if it ran successfull. + # Usage: try + + $* && ok } -# Do a job, write a message, color it and check if it ran successfull. -# @param $1 color -# @param $2 message -# @param $3 command trycm() { + # Do a job, write a message, color it and check if it ran successfull. + # Usage: trycmd + if [[ "$2" != "" ]]; then say sayc "$1" "$2"