parent
d0991b572b
commit
1a9f7bbf1d
96
bashtools.sh
96
bashtools.sh
|
@ -1,5 +1,17 @@
|
|||
#!/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"
|
||||
if [[ $1 == "install" ]]; then
|
||||
mkdir -p "$BASHTOOLS_DIR"
|
||||
|
@ -213,7 +225,23 @@ function getsysstats() {
|
|||
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() {
|
||||
local answer answernum autoselect bestval
|
||||
local cols choice choiceformat cperrow cwidth default defaultnum desc
|
||||
|
@ -303,13 +331,13 @@ function menu() {
|
|||
|
||||
# Validate layout
|
||||
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
|
||||
fi
|
||||
|
||||
# Validate descriptions
|
||||
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
|
||||
fi
|
||||
|
||||
|
@ -322,10 +350,10 @@ function menu() {
|
|||
fi
|
||||
done
|
||||
if [ $defaultnum -eq -1 ]; then
|
||||
cecho "$RED" "ERROR in ask(): given default '$default' does not match any choice. [$question]"
|
||||
echo " choices are:"
|
||||
error "error in myask(): given default '$default' does not match any choice. [$question]"
|
||||
csecho "$RED" " choices are:"
|
||||
for n in ${!choice[@]}; do
|
||||
echo " ${choice[$n]}"
|
||||
csecho "$RED" " ${choice[$n]}"
|
||||
done
|
||||
die
|
||||
fi
|
||||
|
@ -371,13 +399,13 @@ function menu() {
|
|||
fi
|
||||
|
||||
# printf format string
|
||||
choiceformat="%3s) %-$((cwidth-5))s"
|
||||
choiceformat="${GREEN}${BOLD}%3s${PLAIN}${GREEN}) %-$((cwidth-5))s$PLAIN"
|
||||
|
||||
answer=""
|
||||
# Prompt for a choice until we get a valid answer.
|
||||
while [ -z "$answer" ]; do
|
||||
# Display menu
|
||||
echo -e "$question"
|
||||
csecho "$GREEN" "^b$question^p"
|
||||
|
||||
if [ "$layout" == "h" ]; then
|
||||
for n in ${!choice[@]}; do
|
||||
|
@ -425,17 +453,19 @@ function menu() {
|
|||
|
||||
printf "\n"
|
||||
if [ -n "$default" ]; then
|
||||
printf " (* denotes default)\n"
|
||||
csecho "$GREEN" " ^i(* denotes default)^p"
|
||||
fi
|
||||
printf -- "-> "
|
||||
csecho -n "$GREEN" "^b-> ^p"
|
||||
|
||||
# If there's only one answer, just select it. Useful for
|
||||
# cases where we are basing our list of choices on a dynamic variable.
|
||||
if [ $autoselect -eq 1 -a $nchoices -eq 1 ]; then
|
||||
answernum=1
|
||||
echo "1 (autoselect)" # make it look like we typed it
|
||||
csecho "$GREEN" "1 (autoselect)" # make it look like we typed it
|
||||
else
|
||||
echo -e -n "$GREEN"
|
||||
read answernum
|
||||
echo -e -n "$PLAIN"
|
||||
fi
|
||||
|
||||
if [ -z "$answernum" ]; then
|
||||
|
@ -452,7 +482,7 @@ function menu() {
|
|||
done
|
||||
|
||||
else
|
||||
printf "Invalid response.\n\n"
|
||||
csecho "$RED" "Invalid response.\n"
|
||||
fi
|
||||
elif [[ ! $answernum =~ ^[0-9]*$ ]] ; then
|
||||
# try to match based on name
|
||||
|
@ -468,26 +498,25 @@ function menu() {
|
|||
if [[ $found -eq 1 ]]; then
|
||||
selectedposs=${possanswer[0]}
|
||||
selectedpossnum=${possanswernum[0]}
|
||||
answerhilite=${selectedposs/${answernum}/${BOLD}${answernum}${PLAIN}${CYAN}}
|
||||
answerhilite=${selectedposs/${answernum}/^b${answernum}^p}
|
||||
star=""
|
||||
if [[ -z $BOLD ]]; then
|
||||
star="*"
|
||||
fi
|
||||
printf "${CYAN}Matched '${star}${answerhilite}${star}'.${PLAIN}\n\n"
|
||||
csecho "$CYAN" "Matched '${star}${answerhilite}${star}'.\n"
|
||||
answernum="$selectedpossnum"
|
||||
answer="$selectedposs"
|
||||
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
|
||||
echo -e " ${RED}${possanswernum[$z]}) ${possanswer[$z]}"
|
||||
csecho "$RED" " ${possanswernum[$z]}) ${possanswer[$z]}"
|
||||
done
|
||||
echo -e "${PLAIN}"
|
||||
else
|
||||
printf ${RED}"Invalid response.${PLAIN}\n\n"
|
||||
csecho "${RED}" "Invalid response.\n"
|
||||
fi
|
||||
shopt -u nocasematch
|
||||
elif [ $answernum -lt 1 -o $answernum -gt $nchoices ]; then
|
||||
printf "Choice out of range.\n\n"
|
||||
csecho "$RED" "Choice out of range.\n"
|
||||
else
|
||||
answer=${choice[$((answernum - 1))]}
|
||||
fi
|
||||
|
@ -499,21 +528,6 @@ function menu() {
|
|||
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
|
||||
local rv=0 appname errs x
|
||||
appname="$1"
|
||||
|
@ -599,6 +613,20 @@ function dnslookup() { # $1 = a_record_to_look_up
|
|||
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 $(compgen -A function | grep -v ^_)
|
||||
|
||||
|
|
Loading…
Reference in New Issue