Files
hpr_website/www/eps/hpr1757/hpr1757_functions.sh

70 lines
2.0 KiB
Bash
Raw Normal View History

2025-10-28 18:39:57 +01:00
#=== FUNCTION ================================================================
# NAME: pad
# DESCRIPTION: Pad $text on the $side with $char characters to length $length
# PARAMETERS: 1 - the text string to pad (no default)
# 2 - how long the padded string is to be (default 80)
# 3 - the character to pad with (default '-')
# 4 - the side to pad on, L or R or C for centre (default R)
# RETURNS: Nothing
#===============================================================================
pad () {
local text=${1?Usage: pad text [length] [character] [L|R|C]}
local length=${2:-80}
local char=${3:--}
local side=${4:-R}
local line l2
[ ${#text} -ge $length ] && { echo "$text"; return; }
char=${char:0:1}
side=${side^^}
printf -v line "%*s" $(($length - ${#text})) ' '
line=${line// /$char}
if [[ $side == "R" ]]; then
echo "${text}${line}"
elif [[ $side == "L" ]]; then
echo "${line}${text}"
elif [[ $side == "C" ]]; then
l2=$((${#line}/2))
echo "${line:0:$l2}${text}${line:$l2}"
fi
}
#=== FUNCTION ================================================================
# NAME: yes_no
# DESCRIPTION: Read a Yes or No response from STDIN and return a suitable
# numeric value
# PARAMETERS: 1 - Prompt string for the read
# 2 - Default value (optional)
# RETURNS: 0 for a response of Y or YES, 1 otherwise
#===============================================================================
yes_no () {
local prompt="${1:?Usage: yes_no prompt [default]}"
local default="${2// /}"
local ans res
if [[ -n $default ]]; then
default="-i $default"
fi
#
# Read and handle CTRL-D (EOF)
#
read -e $default -p "$prompt" ans
res="$?"
if [[ $res -ne 0 ]]; then
echo "Read aborted"
return 1
fi
ans=${ans^^}
ans=${ans//[^YESNO]/}
if [[ $ans =~ ^Y(E|ES)?$ ]]; then
return 0
else
return 1
fi
}