update to latest commit
This commit is contained in:
parent
96d4df3781
commit
56f7186183
195
bsf.sh
195
bsf.sh
@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# BASH FRAMEWORK
|
# Bash Framework
|
||||||
# Some useful bash functions to make things easier.
|
# Some useful bash functions to make things easier.
|
||||||
#
|
#
|
||||||
# Copyright (C) 2017-2018 willipink.eu
|
# Copyright (C) 2017-2018 willipink.eu
|
||||||
@ -20,28 +20,55 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
LOG=True
|
||||||
|
for arg in $*; do
|
||||||
|
if [[ "$arg" == "--log" ]]; then
|
||||||
|
LOG=False
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
# VARIABLES
|
# VARIABLES
|
||||||
## COLORS (use ANSI escape codes)
|
## COLORS (use ANSI escape codes)
|
||||||
normal () { printf '\033[0m' ; }
|
if [[ "$LOG" == True ]]; then
|
||||||
red () { printf '\033[0;31m' ; }
|
normal () { printf '\033[0m' ; }
|
||||||
green () { printf '\033[0;32m' ; }
|
red () { printf '\033[0;31m' ; }
|
||||||
black () { printf '\033[0;30m' ; }
|
green () { printf '\033[0;32m' ; }
|
||||||
brown () { printf '\033[0;33m' ; }
|
black () { printf '\033[0;30m' ; }
|
||||||
orange () { printf '\033[0;33m' ; }
|
brown () { printf '\033[0;33m' ; }
|
||||||
blue () { printf '\033[0;34m' ; }
|
orange () { printf '\033[0;33m' ; }
|
||||||
purple () { printf '\033[0;35m' ; }
|
blue () { printf '\033[0;34m' ; }
|
||||||
cyan () { printf '\033[0;36m' ; }
|
purple () { printf '\033[0;35m' ; }
|
||||||
gray () { printf '\033[0;37m' ; }
|
cyan () { printf '\033[0;36m' ; }
|
||||||
grey () { printf '\033[1;30m' ; }
|
gray () { printf '\033[0;37m' ; }
|
||||||
lred () { printf '\033[1;31m' ; }
|
grey () { printf '\033[1;30m' ; }
|
||||||
lgreen () { printf '\033[1;32m' ; }
|
lred () { printf '\033[1;31m' ; }
|
||||||
yellow () { printf '\033[1;33m' ; }
|
lgreen () { printf '\033[1;32m' ; }
|
||||||
lblue () { printf '\033[1;34m' ; }
|
yellow () { printf '\033[1;33m' ; }
|
||||||
lpurple (){ printf '\033[1;35m' ; }
|
lblue () { printf '\033[1;34m' ; }
|
||||||
lcyan () { printf '\033[1;36m' ; }
|
lpurple (){ printf '\033[1;35m' ; }
|
||||||
white () { printf '\033[1;37m' ; }
|
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
|
||||||
|
|
||||||
|
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
@ -49,94 +76,88 @@ white () { printf '\033[1;37m' ; }
|
|||||||
# @param string what to say
|
# @param string what to say
|
||||||
say () {
|
say () {
|
||||||
printf %s\\n "$*"
|
printf %s\\n "$*"
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Print to Stdout. Do not finish with a linebreak
|
# Print to Stdout. Do not finish with a linebreak
|
||||||
sayn () {
|
sayn () {
|
||||||
printf %s "$*"
|
printf %s "$*"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# quick styler, usage "style *color*"
|
||||||
|
sayc() {
|
||||||
|
if [[ "$1" == "" ]]; then
|
||||||
|
blue
|
||||||
|
else
|
||||||
|
$1
|
||||||
|
fi
|
||||||
|
sayn " ::: "
|
||||||
|
shift
|
||||||
|
say "$*"
|
||||||
|
normal
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
saycn() {
|
||||||
|
$1
|
||||||
|
sayn " ::: "
|
||||||
|
shift
|
||||||
|
sayn "$*"
|
||||||
|
normal
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# check if user is root
|
# check if user is root
|
||||||
# @return true if user is root, else false
|
# @return true if user is root, else false
|
||||||
is_root () {
|
|
||||||
if [[ "$EUID" -eq 0 ]]; then
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# run a command as root user, if not root, or root password was not yet entered prompt for it
|
|
||||||
# @param *args: arbitrary command to execute as root
|
|
||||||
root () {
|
root () {
|
||||||
if [[ "$EUID" -eq 0 ]]; then
|
if [[ "$EUID" -ne 0 ]]; then
|
||||||
$*
|
say
|
||||||
elif [[ -z $PW ]]; then
|
sayc red "You need to be root."
|
||||||
echo -n "root password: "
|
exit 1
|
||||||
read -s pw
|
|
||||||
echo
|
|
||||||
export "PW=$pw"
|
|
||||||
unset pw
|
|
||||||
fi
|
|
||||||
su -c "$*" <<< "$PW"
|
|
||||||
}
|
|
||||||
|
|
||||||
# ask a question and get the answer
|
|
||||||
# [@param 1 string]: If this is "silent" the user cannot see its keyboard input (good for passwords)
|
|
||||||
# @param 2 string: the prompt text
|
|
||||||
# @return answer: the user input
|
|
||||||
_prompt() {
|
|
||||||
if [[ "$1" == "silent" ]]; then
|
|
||||||
say "$2"
|
|
||||||
read -rs answer
|
|
||||||
else
|
else
|
||||||
say "$1"
|
say
|
||||||
read -r answer
|
sayc green "You are root."
|
||||||
fi
|
|
||||||
}
|
|
||||||
# example _prompt
|
|
||||||
# _prompt "What is your answer?"
|
|
||||||
# say "You responded $answer"
|
|
||||||
|
|
||||||
|
|
||||||
# ask for a yes or no decision
|
|
||||||
# @param 1: the question that should be answered with yes or no
|
|
||||||
# @return: 0 if question was answered with yes, else 0
|
|
||||||
_decide() {
|
|
||||||
until [[ $confirm =~ ^[ynYN]$ ]]; do
|
|
||||||
say "$1"
|
|
||||||
sayn "y(es) or n(o)?: "
|
|
||||||
read -r confirm
|
|
||||||
done
|
|
||||||
if [[ $confirm =~ [yY] ]]; then
|
|
||||||
return 0
|
return 0
|
||||||
else
|
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
# example _decide
|
|
||||||
# if _decide "Time to decide, yes or no?"; then
|
|
||||||
# echo "You decides yes!"
|
|
||||||
# else
|
|
||||||
# echo "You devided no!"
|
|
||||||
# fi
|
|
||||||
|
|
||||||
# Check if last call had errors. If so, print error message and exit.
|
# Check if last call had errors. If so, print error message and exit.
|
||||||
ok() {
|
ok() {
|
||||||
if [[ "$?" -ne 0 ]]; then
|
if [[ "$?" -ne 0 ]]; then
|
||||||
say "ERROR, aborting."
|
sayc red "ERROR, aborting."
|
||||||
exit 1
|
exit 1
|
||||||
|
else
|
||||||
|
sayc green "$0 ... done"
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Do a job and check if it ran successfull.
|
||||||
# quick styler, usage "style *color*"
|
# @param $*: command to execute
|
||||||
sayc() {
|
try() {
|
||||||
[[ "$1" == "" ]] && orange || $1
|
$*
|
||||||
sayn " ::: "
|
if [[ "$?" -ne 0 ]]; then
|
||||||
shift
|
sayc red "ERROR, aborting"
|
||||||
say "$*"
|
say
|
||||||
normal
|
exit 1
|
||||||
|
else
|
||||||
|
sayc green "done"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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 $*
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user