Skip to content

Instantly share code, notes, and snippets.

@jaggzh
Created July 5, 2024 15:32
Show Gist options
  • Save jaggzh/a66f35114ac0c0b3201f43ec09b10f22 to your computer and use it in GitHub Desktop.
Save jaggzh/a66f35114ac0c0b3201f43ec09b10f22 to your computer and use it in GitHub Desktop.
readcountdown1 () {
if [[ "$#" -lt 3 ]]; then
cat <<-EOT
Accepts a single char of user input
Usage: readcountdown1 seconds variable message
Ex: readcountdown1 5 inp '(d)elete (anything else exits)'
gives 5 seconds and sets inp if anything (even enter) entered
returns 0 on success, 1 for timeout
EOT
return 1
fi
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 # for enter
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