Last active
January 15, 2022 10:10
-
-
Save jaggzh/4edda5004fd922d1390e79dbad9d87ac to your computer and use it in GitHub Desktop.
Bash function for a prompt with visible seconds countdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See video: https://youtu.be/5z0JImIiQVs | |
readcountdown1 is a function | |
readcountdown1 () | |
{ | |
local secs="$1"; | |
local var="$2"; | |
local msg="$3"; | |
local uinp; | |
for ((i=secs; i>0; i--)) | |
do | |
unset "$var"; | |
if ! read -r -n 1 -t 1 -p "$msg (${i}s): " uinp; then | |
echo -ne '\r'; | |
else | |
if [[ "$uinp" != '' ]]; then | |
echo; | |
fi; | |
eval "export $var='$uinp'"; | |
return 0; | |
fi; | |
done; | |
if [[ "$i" -lt 1 ]]; then | |
echo; | |
fi; | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment