Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active March 30, 2019 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosfreitas/0a69a3c24a5d7c94c1aaf021997b7055 to your computer and use it in GitHub Desktop.
Save marcosfreitas/0a69a3c24a5d7c94c1aaf021997b7055 to your computer and use it in GitHub Desktop.
press key to continue
function PressKeyToContinue() {
printf "\n";
question="Pressione S para continuar ou qualquer outra tecla para pular essa parte execução do script";
expected_key_pressed='s';
abort_script=false;
if [ ! -z "$1" ]; then
question="${1}";
fi
if [ ! -z "$2" ]; then
expected_key_pressed="${2}";
fi
if [[ ! -z "$3" && "${3,,}" == "abort" ]]; then
# in case of a negative response, the script will abort the process
abort_script=true;
fi
read -n1 -r -p "${question}: " key
# "parameter expansion" transforming key to lowercase
# @info $key is empty when ENTER/SPACE is pressed
if [[ "${key,,}" = "${expected_key_pressed,,}" ]]; then
echo 1;
else
if [ $abort_script == true ]; then
exit 1;
fi
# by default continue to the next step
echo 0;
fi
}
function B() { printf "\nfuncao b"; }
function A() {
local response=
if [[ $(PressKeyToContinue "chamar b? [s ou n]:" "s") -eq 1 ]]; then
B;
printf "\ncontinuou\n";
else
printf "\nnao continuou\n";
fi;
}
function C() { printf "\nfuncao c"; }
A;
C;
printf "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment