parent
d0991b572b
commit
1a9f7bbf1d
96
bashtools.sh
96
bashtools.sh
|
@ -1,5 +1,17 @@
|
||||||
#!/usr/bin/env /bin/bash
|
#!/usr/bin/env /bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
####################################
|
||||||
|
# To include this in a script:
|
||||||
|
#
|
||||||
|
#. ${HOME}/code/bashtools/bashtools.sh
|
||||||
|
#if [[ -z $HAVE_BASHTOOLS ]]; then
|
||||||
|
# echo "ERROR: bashtools not installed download from https://git.nethack.net/rob/bashtools" >/dev/stderr
|
||||||
|
# exit 1
|
||||||
|
#fi
|
||||||
|
#
|
||||||
|
####################################
|
||||||
|
|
||||||
export BASHTOOLS_DIR="${HOME}/.bashtools"
|
export BASHTOOLS_DIR="${HOME}/.bashtools"
|
||||||
if [[ $1 == "install" ]]; then
|
if [[ $1 == "install" ]]; then
|
||||||
mkdir -p "$BASHTOOLS_DIR"
|
mkdir -p "$BASHTOOLS_DIR"
|
||||||
|
@ -213,7 +225,23 @@ function getsysstats() {
|
||||||
DISKFREE_GB=$(echo "$DISKFREE_MB / 1024" | bc )
|
DISKFREE_GB=$(echo "$DISKFREE_MB / 1024" | bc )
|
||||||
}
|
}
|
||||||
|
|
||||||
# menu [ -r RETVAR ] [-R NUMRETVAR] [-a] [-c num] [-l v|h ] [-x AB] [ -q "question text" ] [ -d default ] choice1 choice2 ... choiceN [ -D desc1 desc2 ... descN ]
|
function ask() { # [-s == dont echo input] $1 = prompt $default_val $2 = return_variable_name
|
||||||
|
local answer prompt default retvar readopts=""
|
||||||
|
if [[ $1 == "-s" ]]; then
|
||||||
|
readopts="-s"
|
||||||
|
shift
|
||||||
|
fi
|
||||||
|
prompt="$1"
|
||||||
|
default="$2"
|
||||||
|
retvar="$3"
|
||||||
|
cecho -n -s "$YELLOW" "$1 "
|
||||||
|
read $readopts answer
|
||||||
|
[[ -z $answer ]] && answer="$default"
|
||||||
|
eval "$retvar=\"$answer\""
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ask [ -r RETVAR ] [-R NUMRETVAR] [-a] [-c num] [-l v|h ] [-x AB] [ -q "question text" ] [ -d default ] choice1 choice2 ... choiceN [ -D desc1 desc2 ... descN ]
|
||||||
function menu() {
|
function menu() {
|
||||||
local answer answernum autoselect bestval
|
local answer answernum autoselect bestval
|
||||||
local cols choice choiceformat cperrow cwidth default defaultnum desc
|
local cols choice choiceformat cperrow cwidth default defaultnum desc
|
||||||
|
@ -303,13 +331,13 @@ function menu() {
|
||||||
|
|
||||||
# Validate layout
|
# Validate layout
|
||||||
if [[ ! $layout =~ v|h ]] ; then
|
if [[ ! $layout =~ v|h ]] ; then
|
||||||
cecho "$RED" "ERROR in ask(): invalid layout. Must be 'v'ertical or 'h'orizontal. [$question]"
|
error "error in myask(): invalid layout. Must be 'v'ertical or 'h'orizontal. [$question]"
|
||||||
die
|
die
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Validate descriptions
|
# Validate descriptions
|
||||||
if [ $ndescs -gt $nchoices ]; then
|
if [ $ndescs -gt $nchoices ]; then
|
||||||
cecho "$RED" "ERROR in ask(): Number of descriptions ($ndescs) is larger than number of choices ($nchoices)! [$question]"
|
error "error in myask(): Number of descriptions ($ndescs) is larger than number of choices ($nchoices)! [$question]"
|
||||||
die
|
die
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -322,10 +350,10 @@ function menu() {
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
if [ $defaultnum -eq -1 ]; then
|
if [ $defaultnum -eq -1 ]; then
|
||||||
cecho "$RED" "ERROR in ask(): given default '$default' does not match any choice. [$question]"
|
error "error in myask(): given default '$default' does not match any choice. [$question]"
|
||||||
echo " choices are:"
|
csecho "$RED" " choices are:"
|
||||||
for n in ${!choice[@]}; do
|
for n in ${!choice[@]}; do
|
||||||
echo " ${choice[$n]}"
|
csecho "$RED" " ${choice[$n]}"
|
||||||
done
|
done
|
||||||
die
|
die
|
||||||
fi
|
fi
|
||||||
|
@ -371,13 +399,13 @@ function menu() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# printf format string
|
# printf format string
|
||||||
choiceformat="%3s) %-$((cwidth-5))s"
|
choiceformat="${GREEN}${BOLD}%3s${PLAIN}${GREEN}) %-$((cwidth-5))s$PLAIN"
|
||||||
|
|
||||||
answer=""
|
answer=""
|
||||||
# Prompt for a choice until we get a valid answer.
|
# Prompt for a choice until we get a valid answer.
|
||||||
while [ -z "$answer" ]; do
|
while [ -z "$answer" ]; do
|
||||||
# Display menu
|
# Display menu
|
||||||
echo -e "$question"
|
csecho "$GREEN" "^b$question^p"
|
||||||
|
|
||||||
if [ "$layout" == "h" ]; then
|
if [ "$layout" == "h" ]; then
|
||||||
for n in ${!choice[@]}; do
|
for n in ${!choice[@]}; do
|
||||||
|
@ -425,17 +453,19 @@ function menu() {
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
if [ -n "$default" ]; then
|
if [ -n "$default" ]; then
|
||||||
printf " (* denotes default)\n"
|
csecho "$GREEN" " ^i(* denotes default)^p"
|
||||||
fi
|
fi
|
||||||
printf -- "-> "
|
csecho -n "$GREEN" "^b-> ^p"
|
||||||
|
|
||||||
# If there's only one answer, just select it. Useful for
|
# If there's only one answer, just select it. Useful for
|
||||||
# cases where we are basing our list of choices on a dynamic variable.
|
# cases where we are basing our list of choices on a dynamic variable.
|
||||||
if [ $autoselect -eq 1 -a $nchoices -eq 1 ]; then
|
if [ $autoselect -eq 1 -a $nchoices -eq 1 ]; then
|
||||||
answernum=1
|
answernum=1
|
||||||
echo "1 (autoselect)" # make it look like we typed it
|
csecho "$GREEN" "1 (autoselect)" # make it look like we typed it
|
||||||
else
|
else
|
||||||
|
echo -e -n "$GREEN"
|
||||||
read answernum
|
read answernum
|
||||||
|
echo -e -n "$PLAIN"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "$answernum" ]; then
|
if [ -z "$answernum" ]; then
|
||||||
|
@ -452,7 +482,7 @@ function menu() {
|
||||||
done
|
done
|
||||||
|
|
||||||
else
|
else
|
||||||
printf "Invalid response.\n\n"
|
csecho "$RED" "Invalid response.\n"
|
||||||
fi
|
fi
|
||||||
elif [[ ! $answernum =~ ^[0-9]*$ ]] ; then
|
elif [[ ! $answernum =~ ^[0-9]*$ ]] ; then
|
||||||
# try to match based on name
|
# try to match based on name
|
||||||
|
@ -468,26 +498,25 @@ function menu() {
|
||||||
if [[ $found -eq 1 ]]; then
|
if [[ $found -eq 1 ]]; then
|
||||||
selectedposs=${possanswer[0]}
|
selectedposs=${possanswer[0]}
|
||||||
selectedpossnum=${possanswernum[0]}
|
selectedpossnum=${possanswernum[0]}
|
||||||
answerhilite=${selectedposs/${answernum}/${BOLD}${answernum}${PLAIN}${CYAN}}
|
answerhilite=${selectedposs/${answernum}/^b${answernum}^p}
|
||||||
star=""
|
star=""
|
||||||
if [[ -z $BOLD ]]; then
|
if [[ -z $BOLD ]]; then
|
||||||
star="*"
|
star="*"
|
||||||
fi
|
fi
|
||||||
printf "${CYAN}Matched '${star}${answerhilite}${star}'.${PLAIN}\n\n"
|
csecho "$CYAN" "Matched '${star}${answerhilite}${star}'.\n"
|
||||||
answernum="$selectedpossnum"
|
answernum="$selectedpossnum"
|
||||||
answer="$selectedposs"
|
answer="$selectedposs"
|
||||||
elif [[ $found -gt 1 ]]; then
|
elif [[ $found -gt 1 ]]; then
|
||||||
printf "${RED}Invalid response - '${BOLD}$answernum${PLAIN}${RED}' matches $found answers.${PLAIN}\n\n"
|
csecho "${RED}" "Invalid response - '^b$answernum^p' matches $found answers.\n"
|
||||||
for z in ${!possanswer[@]}; do
|
for z in ${!possanswer[@]}; do
|
||||||
echo -e " ${RED}${possanswernum[$z]}) ${possanswer[$z]}"
|
csecho "$RED" " ${possanswernum[$z]}) ${possanswer[$z]}"
|
||||||
done
|
done
|
||||||
echo -e "${PLAIN}"
|
|
||||||
else
|
else
|
||||||
printf ${RED}"Invalid response.${PLAIN}\n\n"
|
csecho "${RED}" "Invalid response.\n"
|
||||||
fi
|
fi
|
||||||
shopt -u nocasematch
|
shopt -u nocasematch
|
||||||
elif [ $answernum -lt 1 -o $answernum -gt $nchoices ]; then
|
elif [ $answernum -lt 1 -o $answernum -gt $nchoices ]; then
|
||||||
printf "Choice out of range.\n\n"
|
csecho "$RED" "Choice out of range.\n"
|
||||||
else
|
else
|
||||||
answer=${choice[$((answernum - 1))]}
|
answer=${choice[$((answernum - 1))]}
|
||||||
fi
|
fi
|
||||||
|
@ -499,21 +528,6 @@ function menu() {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function ask() { # [-s == dont echo input] $1 = prompt $default_val $2 = return_variable_name
|
|
||||||
local answer prompt default retvar readopts=""
|
|
||||||
if [[ $1 == "-s" ]]; then
|
|
||||||
readopts="-s"
|
|
||||||
shift
|
|
||||||
fi
|
|
||||||
prompt="$1"
|
|
||||||
default="$2"
|
|
||||||
retvar="$3"
|
|
||||||
cecho -n -s "$YELLOW" "$1 "
|
|
||||||
read $readopts answer
|
|
||||||
[[ -z $answer ]] && answer="$default"
|
|
||||||
eval "$retvar=\"$answer\""
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkreqs() { # appname cpus ram_in_gb disk_in_gb
|
function checkreqs() { # appname cpus ram_in_gb disk_in_gb
|
||||||
local rv=0 appname errs x
|
local rv=0 appname errs x
|
||||||
appname="$1"
|
appname="$1"
|
||||||
|
@ -599,6 +613,20 @@ function dnslookup() { # $1 = a_record_to_look_up
|
||||||
return $rv
|
return $rv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function profile() {
|
||||||
|
local now str diff
|
||||||
|
[[ -z $__PROFILE || $__PROFILE -ne 1 ]] && return;
|
||||||
|
#now=$(date +%Y/%m/%d-%H:%M:%S)
|
||||||
|
now=$(printf "%0.f" "$(bc <<<"$(gdate +"%s.%N")*1000")")
|
||||||
|
str="^b${FUNCNAME[1]}()^p ===> ^b$*^p"
|
||||||
|
if [[ -n $__PROF_PREV ]]; then
|
||||||
|
diff=$(echo "scale=2; ($now - $__PROF_PREV)" | bc)
|
||||||
|
str="$str $GREEN($BOLD+$diff ms$PLAIN$GREEN)^p"
|
||||||
|
fi
|
||||||
|
cecho -s "${MAGENTA}" "$str" >&2
|
||||||
|
__PROF_PREV=$now
|
||||||
|
}
|
||||||
|
|
||||||
#declare -fx $(bash -c "source $0 &> /dev/null; compgen -A function")
|
#declare -fx $(bash -c "source $0 &> /dev/null; compgen -A function")
|
||||||
declare -fx $(compgen -A function | grep -v ^_)
|
declare -fx $(compgen -A function | grep -v ^_)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue