2 ms·
Plus `ask_yes` if you want to ask if user is sure. ask_yes () { read -r -p "$1 = Are you sure? [y/N] " response if [[ "$response" =~ ^([yY][eE][sS]|
by gr4viton 8y ago
Plus `ask_yes` if you want to ask if user is sure.
ask_yes () {
read -r -p "$1 = Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
return 0 # true = 0
else
return 1 # false = 1
fi
}
Plus `countdown timer` to make user think about what he has done, before he does it. (Code is a mess cause i used some snippet then created another from it)
countdown_str () {
# countdown "00:01:00" # = hh:mm:ss
IFS=:
set -- $*
secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} ))
while [ $secs -gt 0 ]
do
sleep 1 &
printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60))
secs=$(( $secs - 1 ))
wait
done
echo
}
countdown_5 () {
countdown 5 2>/dev/null
}
countdown () {
secs=${1:-"00"}
mins=${2:-"00"}
hours=${3:-"00"}
chars=$(printf "%02d:%02d:%02d" $hours $mins $secs)
echo "Countdown from $chars"
countdown_str $chars 2>/dev/null
}