From 96d4df37812c0c976100bf65f238870307692285 Mon Sep 17 00:00:00 2001 From: koksnuss Date: Sat, 2 Mar 2019 02:47:48 +0100 Subject: [PATCH] add prompt and decide function --- bsf.sh | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 113 insertions(+), 16 deletions(-) diff --git a/bsf.sh b/bsf.sh index 13ac63b..730903f 100644 --- a/bsf.sh +++ b/bsf.sh @@ -1,23 +1,28 @@ #!/bin/bash -##### bash framework -# some useful bash functions to make things easier +# BASH FRAMEWORK +# 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 . -## use printf for safety purposes -# http://www.etalabs.net/sh_tricks.html -# @param string what to say -say () { printf %s\\n "$*" ; } -sayn () { printf %s "$*" ; } -## check if user is root -root () { [[ "$EUID" -eq 0 ]] ; } -## check if last call had errors -## if yes print generic error message and exit -ok() { [[ "$?" -ne 0 ]] && echo "ERROR, aborting." && exit 1; } - -## colors -# use ANSI escape codes +# VARIABLES +## COLORS (use ANSI escape codes) normal () { printf '\033[0m' ; } red () { printf '\033[0;31m' ; } green () { printf '\033[0;32m' ; } @@ -37,9 +42,101 @@ lpurple (){ printf '\033[1;35m' ; } lcyan () { printf '\033[1;36m' ; } white () { printf '\033[1;37m' ; } + + +# 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 "$*" +} + + +# Print to Stdout. Do not finish with a linebreak +sayn () { + printf %s "$*" +} + + +# check if user is root +# @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 () { + if [[ "$EUID" -eq 0 ]]; then + $* + elif [[ -z $PW ]]; then + echo -n "root password: " + 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 + say "$1" + read -r answer + 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 + else + return 1 + 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. +ok() { + if [[ "$?" -ne 0 ]]; then + say "ERROR, aborting." + exit 1 + fi +} + + # quick styler, usage "style *color*" -style() { +sayc() { [[ "$1" == "" ]] && orange || $1 sayn " ::: " + shift + say "$*" normal }