bsf/bsf.sh

164 lines
3.6 KiB
Bash
Raw Normal View History

2018-07-11 20:57:52 +02:00
#!/bin/bash
2019-05-28 23:13:07 +02:00
# Bash Framework
2019-03-02 02:47:48 +01:00
# Some useful bash functions to make things easier.
#
# Copyright (C) 2017-2018 willipink.eu
# Author Moritz Münch moritzmuench@mailbox.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-07-11 20:57:52 +02:00
2019-05-28 23:13:07 +02:00
LOG=True
for arg in $*; do
if [[ "$arg" == "--log" ]]; then
LOG=False
fi
done
2019-03-02 02:47:48 +01:00
# VARIABLES
## COLORS (use ANSI escape codes)
2019-05-28 23:13:07 +02:00
if [[ "$LOG" == True ]]; then
normal () { printf '\033[0m' ; }
red () { printf '\033[0;31m' ; }
green () { printf '\033[0;32m' ; }
black () { printf '\033[0;30m' ; }
brown () { printf '\033[0;33m' ; }
orange () { printf '\033[0;33m' ; }
blue () { printf '\033[0;34m' ; }
purple () { printf '\033[0;35m' ; }
cyan () { printf '\033[0;36m' ; }
gray () { printf '\033[0;37m' ; }
grey () { printf '\033[1;30m' ; }
lred () { printf '\033[1;31m' ; }
lgreen () { printf '\033[1;32m' ; }
yellow () { printf '\033[1;33m' ; }
lblue () { printf '\033[1;34m' ; }
lpurple (){ printf '\033[1;35m' ; }
lcyan () { printf '\033[1;36m' ; }
white () { printf '\033[1;37m' ; }
else
normal () { return; }
red () { return; }
green () { return; }
black () { return; }
brown () { return; }
orange () { return; }
blue () { return; }
purple () { return; }
cyan () { return; }
gray () { return; }
grey () { return; }
lred () { return; }
lgreen () { return; }
yellow () { return; }
lblue () { return; }
lpurple (){ return; }
lcyan () { return; }
white () { return; }
fi
2019-03-02 02:47:48 +01:00
# FUNCTIONS
# Print somethong to STDOUT. Use printf for safety purposes, see http://www.etalabs.net/sh_tricks.html
# @param string what to say
say () {
printf %s\\n "$*"
2019-05-28 23:13:07 +02:00
return 0
2019-03-02 02:47:48 +01:00
}
# Print to Stdout. Do not finish with a linebreak
sayn () {
printf %s "$*"
2019-05-28 23:13:07 +02:00
return 0
2019-03-02 02:47:48 +01:00
}
2019-05-28 23:13:07 +02:00
# quick styler, usage "style *color*"
sayc() {
if [[ "$1" == "" ]]; then
blue
2019-03-02 02:47:48 +01:00
else
2019-05-28 23:13:07 +02:00
$1
2019-03-02 02:47:48 +01:00
fi
2019-05-28 23:13:07 +02:00
sayn " ::: "
shift
say "$*"
normal
return 0
2019-03-02 02:47:48 +01:00
}
2019-05-28 23:13:07 +02:00
saycn() {
$1
sayn " ::: "
shift
sayn "$*"
normal
return 0
2019-03-02 02:47:48 +01:00
}
2019-05-28 23:13:07 +02:00
# check if user is root
# @return true if user is root, else false
root () {
if [[ "$EUID" -ne 0 ]]; then
say
sayc red "You need to be root."
exit 1
2019-03-02 02:47:48 +01:00
else
2019-05-28 23:13:07 +02:00
say
sayc green "You are root."
return 0
2019-03-02 02:47:48 +01:00
fi
}
2019-05-28 23:13:07 +02:00
# Check if last call had errors. If so, print error message and exit.
ok() {
if [[ "$?" -ne 0 ]]; then
sayc red "ERROR, aborting."
exit 1
2019-03-02 02:47:48 +01:00
else
2019-05-28 23:13:07 +02:00
sayc green "$0 ... done"
return 0
2019-03-02 02:47:48 +01:00
fi
}
2019-05-28 23:13:07 +02:00
# Do a job and check if it ran successfull.
# @param $*: command to execute
try() {
$*
2019-03-02 02:47:48 +01:00
if [[ "$?" -ne 0 ]]; then
2019-05-28 23:13:07 +02:00
sayc red "ERROR, aborting"
say
2019-03-02 02:47:48 +01:00
exit 1
2019-05-28 23:13:07 +02:00
else
sayc green "done"
return 0
2019-03-02 02:47:48 +01:00
fi
}
2019-05-28 23:13:07 +02:00
# Do a job, write a message, color it and check if it ran successfull.
# @param $1 color
# @param $2 message
# @param $3 command
trycm() {
if [[ "$2" != "" ]]; then
say
sayc "$1" "$2"
fi
shift && shift
saycn normal "$*"
try $*
2018-07-11 20:57:52 +02:00
}