A fancy 'are you sure' style shell prompt
fancyprompt take at least two args, the prompt message and produces an 'are you
sure' style prompt with a timeout of 10 sec and option to cancel with 'c'.
The first arg is always the message as a string (accepts \n \t etc). And the
remaining args are function names that are called depending on the result.
-
when 2 args:
- arg2 is function called for non-cancels ('c' is not received)
-
when 3 args:
- arg2 is function called for non-cancels that timeout or [enter] is hit
- arg2 is function called for non-cancels that have a custom reply
-
when 4 args:
- arg2 is function called for non-cancels that timeout or [enter] is hit
- arg2 is function called for cancels ('c' is received)
functions will receive the reply as their argument only when relevent.
TIP 1: You don't want arg 3 but you want arg 4, put the same for arg 2 and 3
__TIP 2: TO abort script for 'c', just put exit as last arg!
TIP 3: Place dummies (empty strings) for args. See demo!
TIP 4: uncomment the last line and run this file
Default timeout is 10 second which can be changed with a third argument.
NOTE
The read reply
must be outside the function or else the prompt won't print until after reply (or timeout).
BEWARE
the prompt_w_timeout
function uses a global variable $reply which is emptied at start of execution.
This is so you can have the read
outside of the function.
IMPORTANT
Arg 1 takes a string and accepts \n
so you'll likely feed it a variable. Be sure to wrap it like this
"$variable"
IN QUOTES or it won't work.
BUT ACTUALLY...
You don't even need to give it any args if you create a variable called $prompt
. It will use that if no $1
prompt="Proceeding to kill you in 10 seconds\nType 'c' and enter to live "
prompt="${prompt}'d' and enter to defend \n or just hit enter to die now"
prompt_w_timeout
read reply
if [[ $reply == 'c' ]]; then
echo "You shall live."
elif [[ "$reply" == 'd' ]]; then
echo "You live. I dead."
else
echo "You die"
fi
print some ascii dividers without cluttering your scripts. output:
###############################################################
######## ATTENTION BAR
stuff is happening
=============================================================
======== ALERT PIPE
stuff is happening
___________________________________________________________
________ hey... just a line
stuff happens
note to self: "shellshots"