From 4a975db1c0fc658034370ad96a994b229c95c860 Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Fri, 18 Nov 2022 13:43:10 +0100 Subject: [PATCH] added disp function and use it --- profile.d/compress.sh | 80 ++++++++++++++++--------------- profile.d/debug.sh | 6 +-- profile.d/disp.sh | 107 ++++++++++++++++++++++++++++++++++++++++++ profile.d/filefct.sh | 12 ++--- profile.d/info.sh | 4 +- profile.d/net.sh | 8 ++-- profile.d/packages.sh | 6 +-- profile.d/pwd.sh | 16 +++---- profile.d/ssh.sh | 12 ++--- profile.sh | 7 +-- 10 files changed, 184 insertions(+), 74 deletions(-) create mode 100644 profile.d/disp.sh diff --git a/profile.d/compress.sh b/profile.d/compress.sh index e1bd540..1e1aa90 100644 --- a/profile.d/compress.sh +++ b/profile.d/compress.sh @@ -33,7 +33,7 @@ utaz() ;; "-"*) - echo "Invalid option, use \"utaz --help\" to display options list" + disp E "Invalid option, use \"utaz --help\" to display options list" echo return 1 ;; @@ -45,55 +45,54 @@ utaz() esac done - [[ $createdir && $nodir ]] && echo "*** Error: --create-dir and --no-dir options are mutually exclusive." + [[ $createdir && $nodir ]] && disp E "The --create-dir and --no-dir options are mutually exclusive." [[ ! $LIST ]] && local LIST="." for zitem in $LIST; do [[ $(ls $zitem/*.zip 2> /dev/null | wc -l) -eq 0 ]] && - echo "$zitem contains no supported archive file, skipping." && + disp W "$zitem contains no supported archive file, skipping." && continue for f in $zitem/*.zip; do - echo -n "Processing archive $zitem/$f... " + disp I "Processing archive $zitem/$f... " local dir=${f::-4} mkdir -p $dir [[ $? -gt 0 ]] && - echo "[ filesystem can't create directories, exit ]" && + disp E "The filesystem can't create directories, exit!" && return 1 unzip -o $f -d $dir > /dev/null 2>&1 case $? in 0) - [[ $willrm ]] && rm -f $f && echo -n "Deleted ! " + [[ $willrm ]] && rm -f $f && disp I "File $zitem/$f deleted." ;; 1) - echo "No deletion on warnings " + disp W "Compression program returned a warning: deletion canceled." ;; *) - echo "[ zip file corrupted, failed ]" + disp E "The zip file seems corrupted, failed." rm -rf $dir > /dev/null 2>&1 continue ;; esac if [[ $createdir ]]; then - echo -n "[ subdir created, " + disp I "Archive extracted successfully in subdirectory." elif [[ $nodir ]]; then mv ./$dir/* ./ && rmdir $dir - echo -n "[ No subdir, " + disp I "Archive extracted successfully, no subdirectory needed." else subdirs=$(find $dir -maxdepth 1 | wc -l) if [[ $subdirs -eq 2 ]]; then mv ./$dir/* ./ && rmdir $dir - echo -n "[ No subdir, " + disp I "Archive extracted successfully, no subdirectory needed." else - echo -n "[ subdir created, " + disp I "Archive extracted successfully in subdirectory." fi fi - echo " OK ]" done done } @@ -107,15 +106,15 @@ taz () _doxz() { command -v xz >/dev/null 2>&1 || { - echo -e >&2 "\t*** The program 'xz' is not installed, aborting." + disp E "The program 'xz' is not installed, aborting." return 127 } [[ $4 ]] && local verb='-v' # Display a warning for this format - echo -e "\t! Warning: xz format is not suited for long term archiving." - echo -e "\t See https://www.nongnu.org/lzip/xz_inadequate.html for details." + disp W "xz format is not suited for long term archiving." + disp I "See https://www.nongnu.org/lzip/xz_inadequate.html for details." # Compresse to xz (lzma2) - Deprecated xz $verb --compress --keep -$3 -T $2 $1 @@ -129,14 +128,14 @@ taz () command -v plzip >/dev/null 2>&1 || { command -v lzip >/dev/null 2>&1 || { - echo -e >&2 "\t*** Program 'plzip' or 'lzip' are not installed, aborting." + disp E "Program 'plzip' or 'lzip' are not installed, aborting." return 127 } local command=lzip local procopt="" [[ $2 -gt 1 ]] && - echo -e "\t! Warning: lzip doesn't support multithreading, falling back to 1 thread." && - echo -e "\t* Consitder installing plzip to obtain multithreading abilities." + disp W "lzip doesn't support multithreading, falling back to 1 thread." && + disp W "Consitder installing plzip to obtain multithreading abilities." } [[ $4 ]] && local verb="-vv" @@ -153,14 +152,14 @@ taz () command -v pigz >/dev/null 2>&1 || { command -v gzip >/dev/null 2>&1 || { - echo -e >&2 "\t*** Programs 'pigz' or 'gzip' are not installed, aborting." + disp E "Programs 'pigz' or 'gzip' are not installed, aborting." return 127 } local command="gzip --compress" local procopt="" [[ $2 -gt 1 ]] && - echo -e "\t! Warning: gzip doesn't support multithreading, falling back to 1 thread." && - echo -e "\t* Consitder installing pigz to obtain multithreading abilities." + disp W "gzip doesn't support multithreading, falling back to 1 thread." && + disp W "Consitder installing pigz to obtain multithreading abilities." } [[ $4 ]] && local verb="--verbose" @@ -177,14 +176,14 @@ taz () command -v pbzip2 >/dev/null 2>&1 || { command -v bzip2 >/dev/null 2>&1 || { - echo -e >&2 "\t*** The program 'pbzip2' or 'bzip2' are not installed, aborting." + disp E "The program 'pbzip2' or 'bzip2' are not installed, aborting." return 127 } local command=bzip2 local procopt="" [[ $2 -gt 1 ]] && - echo -e "\t! Warning: bzip2 doesn't support multithreading, falling back to 1 thread." && - echo -e "\t* Consitder installing pbzip2 to obtain multithreading abilities." + disp W "bzip2 doesn't support multithreading, falling back to 1 thread." && + disp W "Consitder installing pbzip2 to obtain multithreading abilities." } [[ $4 ]] && local verb="-v" @@ -197,12 +196,12 @@ taz () _dolzo() { command -v lzop >/dev/null 2>&1 || { - echo -e >&2 "\t*** The program 'lzop' is not installed, aborting." + disp E "The program 'lzop' is not installed, aborting." return 127 } [[ $4 ]] && local verb='-v' - [[ $2 -gt 1 ]] && echo -e "\t! Warning: lzop doesn't support multithreading, falling back to 1 thread." + [[ $2 -gt 1 ]] && disp W "lzop doesn't support multithreading, falling back to 1 thread." # Compresse au format lzo lzop --keep -$3 $1 @@ -223,6 +222,7 @@ taz () echo " given, the smalest is kept" echo " -p, --parallel Number of threads to use (if allowed by underlying utility)" echo " -v, --verbose Display progress where possible" + echo " -q, --quiet Display less messages (only errors and warnings)" echo " -1, .., -9 Compression level to use [1=fast/big, 9=slow/small]" echo echo "Supported archive format:" @@ -254,6 +254,9 @@ taz () local verbose=1 ;; + "-q"|"--quiet") + QUIET=1 + "-"[1..9]) local complevel=$(echo $opt | sed 's/-//') ;; @@ -273,22 +276,23 @@ taz () [[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed) [[ ! $nproc ]] && nproc=1 [[ ! $complevel ]] && complevel=6 + [[ $verbose -gt 1 && $QUIET -gt 1 ]] && + disp E "The --verbose and --quiet options can't be used together." for item in $LIST; do local donetar=0 - echo "--- Processing $item..." + disp I "Processing $item..." if [[ -d $item ]]; then - echo -ne "\t* Creating $item.tar... " + disp I "\t Creating $item.tar... " tar -cf $item{.tar,} if [[ ! $? -eq 0 ]]; then - echo "[ failed, skipping ]" + disp E "tar file creation failed, skipping to next item." continue fi local donetar=1 - echo "[ OK ]" fi local fname=$item @@ -296,15 +300,15 @@ taz () # Skip compression part if tar is asked if [[ $compform != "tar" ]]; then - echo -e "\t* Compressing archive..." + disp I "\t Compressing archive..." _do$compform $fname $nproc $complevel $verbose [[ ! $? -eq 0 ]] && case $? in 127) - echo -e "\t*** Compression program unavailable, aborting." + disp E "Compression program unavailable, aborting." return 127 ;; *) - echo -e "\t*** Compression program returned an error, not deleting anything if asked, skipping to next item." + disp E "Compression program returned an error, not deleting anything if asked, skipping to next item." continue ;; esac @@ -313,12 +317,10 @@ taz () fi if [[ $willrm ]]; then - echo -en "\t* Deleting original source as asked... " - rm -r $item && echo '[ OK ]' || echo '[ failed ]' + disp I "\t Deleting original source as asked... " + rm -r $item fi - - echo "--- Done" done - + unset QUIET } export -f taz diff --git a/profile.d/debug.sh b/profile.d/debug.sh index ee751b0..fab3e3c 100644 --- a/profile.d/debug.sh +++ b/profile.d/debug.sh @@ -52,18 +52,18 @@ settrace () ;; "--on") if [[ $status == "on" ]]; then - echo "Warning: ERR signal trap is already set, replacing previous trap!" + disp W "ERR signal trap is already set, replacing previous trap!" fi trap "error" ERR ;; "--off") if [[ $status != "on" ]]; then - echo "Warning: ERR signal trap is already unset!" + disp W "ERR signal trap is already unset!" fi trap - ERR ;; "--status") - echo "ERR trap signal is ${status}." + disp "ERR trap signal is ${status}." ;; esac done diff --git a/profile.d/disp.sh b/profile.d/disp.sh new file mode 100644 index 0000000..41902ab --- /dev/null +++ b/profile.d/disp.sh @@ -0,0 +1,107 @@ +# Color definitions +# ------------------------------------------------------------------------------ +# Standard 16 colors display declaration +export DEFAULTFG="\e[0;39m" +export DEFAULTBG="\e[0;49m" +export DEFAULTCOL=${DEFAULTBG}${DEFAULTFG} + +# Regular Colors +export Black='\e[0;30m' +export Red='\e[0;31m' +export Green='\e[0;32m' +export Yellow='\e[0;33m' +export Blue='\e[0;34m' +export Purple='\e[0;35m' +export Cyan='\e[0;36m' +export White='\e[0;37m' + +# Bold +export BBlack='\e[1;30m' +export BRed='\e[1;31m' +export BGreen='\e[1;32m' +export BYellow='\e[1;33m' +export BBlue='\e[1;34m' +export BPurple='\e[1;35m' +export BCyan='\e[1;36m' +export BWhite='\e[1;37m' + +# Underline +export UBlack='\e[4;30m' +export URed='\e[4;31m' +export UGreen='\e[4;32m' +export UYellow='\e[4;33m' +export UBlue='\e[4;34m' +export UPurple='\e[4;35m' +export UCyan='\e[4;36m' +export UWhite='\e[4;37m' + +# Background +export On_Black='\e[40m' +export On_Red='\e[41m' +export On_Green='\e[42m' +export On_Yellow='\e[43m' +export On_Blue='\e[44m' +export On_Purple='\e[45m' +export On_Cyan='\e[46m' +export On_White='\e[47m' + +# High Intensity +export IBlack='\e[0;90m' +export IRed='\e[0;91m' +export IGreen='\e[0;92m' +export IYellow='\e[0;93m' +export IBlue='\e[0;94m' +export IPurple='\e[0;95m' +export ICyan='\e[0;96m' +export IWhite='\e[0;97m' + +# Bold High Intensity +export BIBlack='\e[1;90m' +export BIRed='\e[1;91m' +export BIGreen='\e[1;92m' +export BIYellow='\e[1;93m' +export BIBlue='\e[1;94m' +export BIPurple='\e[1;95m' +export BICyan='\e[1;96m' +export BIWhite='\e[1;97m' + +# High Intensity backgrounds +export On_IBlack='\e[0;100m' +export On_IRed='\e[0;101m' +export On_IGreen='\e[0;102m' +export On_IYellow='\e[0;103m' +export On_IBlue='\e[0;104m' +export On_IPurple='\e[0;105m' +export On_ICyan='\e[0;106m' +export On_IWhite='\e[0;107m' + + +# ------------------------------------------------------------------------------ +# Display a message +# ------------------------------------------------------------------------------ +disp() +{ + case $1 in + "I") + local heads="[ ${IGreen}info${DEFAULTFG} ]" + [[ -z $QUIET || $QUIET -ne 1 ]] && echo -e "${heads} $@" + ;; + "W") + local heads="[ ${IYellow}Warning${DEFAULTFG} ]" + echo -e "${heads} $@" >&2 + ;; + "E") + local heads="[ ${IRed}ERROR${DEFAULTFG} ]" + echo -e "${heads} $@" >&2 + ;; + "D") + local heads"[ ${ICyan}debug${DEFAULTFG} ]" + [[ -n $DEBUG && $DEBUG -gt 1 ]] && echo -e "${heads} $@" + ;; + "*") + local heads="" + [[ -z $QUIET || $QUIET -ne 1 ]] && echo -e "$@" + ;; + esac + unset heads +} diff --git a/profile.d/filefct.sh b/profile.d/filefct.sh index 4d4974e..36c306c 100644 --- a/profile.d/filefct.sh +++ b/profile.d/filefct.sh @@ -47,7 +47,7 @@ clean () ;; "-"*) - echo "Invalid option, use \"clean --help\" to display usage." + disp E "Invalid option, use \"clean --help\" to display usage." echo return 1 ;; @@ -86,8 +86,8 @@ export -f clean mcd () { if [[ ! $# -eq 1 ]] ; then - echo "Create a directory then goes inside." - echo "Usage: mcd " + disp E "Create a directory then goes inside." + disp E "Usage: mcd " return 1 fi mkdir -pv $1 && cd $1 @@ -142,7 +142,7 @@ rmspc () ;; *) - echo "Invalid parameter, use \"rmspc --help\" to display options list" + disp E "Invalid parameter, use \"rmspc --help\" to display options list" echo return 1 ;; @@ -155,12 +155,12 @@ rmspc () for f in *; do [[ $recurs ]] && [[ -d "$f" ]] && ( - [[ $verb ]] && echo "-- Entering directory $(pwd)/$f ..." + [[ $verb ]] && disp I "Entering directory $(pwd)/$f ..." local lastdir=$f pushd "$f" > /dev/null rmspc $@ popd > /dev/null - [[ $verb ]] && echo "-- Leaving directory $(pwd)/$lastdir" + [[ $verb ]] && disp I "Leaving directory $(pwd)/$lastdir" unset lastdir ) diff --git a/profile.d/info.sh b/profile.d/info.sh index 1e33052..09e96f0 100644 --- a/profile.d/info.sh +++ b/profile.d/info.sh @@ -3,7 +3,7 @@ # ------------------------------------------------------------------------------ ver () { - echo "Profile version $PROFVERSION." + disp "Profile version $PROFVERSION." } export -f ver @@ -17,7 +17,7 @@ meteo () [[ $# -eq 0 ]] && local cities=$DEFAULT_CITY for city in $cities; do - curl https://wttr.in/$city || echo "Failed fetching datas for $city." + curl https://wttr.in/$city || disp E "Failed fetching datas for $city." done } export -f meteo diff --git a/profile.d/net.sh b/profile.d/net.sh index 3b7d6b0..6ef2f4f 100644 --- a/profile.d/net.sh +++ b/profile.d/net.sh @@ -15,13 +15,13 @@ isipv4 () if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then if [[ -t 1 ]]; then - echo "The given IPv4 is valid." + disp "The given IPv4 is valid." fi return 0 fi fi if [[ -t 1 ]]; then - echo "The given parameter is NOT a valid IPv4." + disp "The given parameter is NOT a valid IPv4." fi return 1 } @@ -37,12 +37,12 @@ isipv6 () local regex='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$' if [[ $ip =~ $regex ]]; then if [[ -t 1 ]]; then - echo "The given IPv6 is valid." + disp "The given IPv6 is valid." fi return 0 fi if [[ -t 1 ]]; then - echo "The given parameter is not a valid IPv6." + disp "The given parameter is not a valid IPv6." fi return 1 } diff --git a/profile.d/packages.sh b/profile.d/packages.sh index 1980333..eb78d8b 100644 --- a/profile.d/packages.sh +++ b/profile.d/packages.sh @@ -23,18 +23,18 @@ dpkgs () local pkg=$1 && shift count=$(( $count + 1 )) [[ $count -gt 1 ]] && - echo "*** Error: Please specify a package name, without space, eventually partial." && + disp E "Please specify a package name, without space, eventually partial." && return 1 ;; esac done [[ $count -lt 1 ]] && - echo "*** Error: Please specify a package name, without space, eventually partial." && + disp E "Please specify a package name, without space, eventually partial." && return 1 [[ -x /usr/sbin/dpkg ]] && - echo "*** Error: dpkg command seems unavialable." && + disp E "dpkg command seems unavialable." && return 2 dpkg -l | grep $pkg diff --git a/profile.d/pwd.sh b/profile.d/pwd.sh index d9fed47..9fcf5c9 100755 --- a/profile.d/pwd.sh +++ b/profile.d/pwd.sh @@ -37,7 +37,7 @@ genpwd() echo "and thus, cannot be used without provoquing errors. Those identified caracters" echo "are :" echo ' * ? \ $ { }' - echo + echo return 0 ;; "-s"|"--nosymbols") @@ -58,24 +58,24 @@ genpwd() "-L"?*|"--length"?*) local length=$(echo "$opt" | cut -f 2- -d '=') if ! [[ $length =~ ^[0-9]+$ ]]; then - echo "The --length parameter requires a number." + disp E "The --length parameter requires a number." return 1 fi ;; "-o"?*|"--occurences"?*) local occurs=$(echo "$opt" | cut -f 2- -d '=') if ! [[ $occurs =~ ^[1-9]+$ ]]; then - echo "The --occurs parameter requires a number from 1 to 9." + disp E "The --occurs parameter requires a number from 1 to 9." return 1 fi ;; "-*") - echo "Unknow parameter ${opt}." + disp E "Unknow parameter ${opt}." return 1 ;; *) if ! [[ $opt =~ ^[1-9]+$ ]]; then - echo "Unknow parameter ${opt}." + disp E "Unknow parameter ${opt}." return 1 else local nbpwd=$opt @@ -99,7 +99,7 @@ genpwd() echo "$char" } - echo "Generating $nbpwd passwords, please wait..." + disp I "Generating $nbpwd passwords, please wait..." for n in $( seq 1 $nbpwd ); do { local carset='' # store final caracter set to use @@ -135,8 +135,8 @@ genpwd() # Check if we have enough car to have something viable if [[ ${#carset} -lt $length ]]; then - echo 'Error: not enought caracters are authorised for the password length.' - echo 'Please allow more caracter (preferably) or reduce password lentgh.' + disp E 'Not enought caracters are authorised for the password length.' + disp E 'Please allow more caracter (preferably) or reduce password lentgh.' return 1 fi diff --git a/profile.d/ssh.sh b/profile.d/ssh.sh index a60673c..7b43f99 100644 --- a/profile.d/ssh.sh +++ b/profile.d/ssh.sh @@ -4,8 +4,8 @@ rmhost () { if [[ "$#" -lt 1 ]]; then - echo "Error: incorrect number of parameters." - echo "Usage: rmhost [hostname2|ip2 [...]]" + disp E "Incorrect number of parameters." + disp E "Usage: rmhost [hostname2|ip2 [...]]" return 1 fi @@ -25,16 +25,16 @@ rmhost () if [[ ! $ip && $hst ]]; then ip=$(host $hst | grep "has address" | awk '{print $NF}') [[ ! $? ]] && - echo "*** rmhost(): Error extracting IP from hostname." && + disp E "Impossible to extract IP from hostname." && return 1 fi if [[ $hst ]]; then - echo "Removing host $hst from ssh known_host..." + disp I "Removing host $hst from ssh known_host..." ssh-keygen -R $hst > /dev/null fi if [[ $ip ]]; then - echo "Removing IP $ip from ssh known_host..." + disp I "Removing IP $ip from ssh known_host..." ssh-keygen -R $ip > /dev/null fi unset hst ip @@ -60,7 +60,7 @@ ssr () done [[ ! $1 ]] && - echo "Please specify the server you want to log in." && + disp E "Please specify the server you want to log in." && return 1 local srv=$1 && shift diff --git a/profile.sh b/profile.sh index 8bdbf71..b33ae48 100644 --- a/profile.sh +++ b/profile.sh @@ -36,6 +36,7 @@ # 07/11/2022 v3.0.1 : added concatenation to rmspc, added ku, error managed in meteo # 08/11/2022 v3.1.0 : added password generator # 10/11/2022 v3.1.1 : genpwd: test if password is doable +# 18/11/2022 v3.2.0 : created disp command for display, make use of it # ------------------------------------------------------------------------------ # Copyright (c) 2013-2022 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. @@ -76,8 +77,8 @@ export PROFVERSION="3.1.1" export DEFAULT_CITY="Toulouse" if [[ ! $(echo $SHELL | grep bash) ]]; then - echo "That script is designed to be used with bash as being the shell." - echo "Please consider using bash instead, or patch me ;) !" + echo "That environmet script is designed to be used with bash or zsh being the shell." + echo "Please consider using bash or zsh instead, or patch me ;)!" return 1 fi @@ -196,7 +197,7 @@ if [[ $INTERACTIVE ]]; then # Set default language setfr showinfo - echo "Profile version $PROFVERSION chargé..." + disp I "Profile version $PROFVERSION chargé..." fi # Cleanup