One alternative that might be cleaner is to have answer
return 0 or return 1, depending on whether the user said yes
or no
. Then test the value of answer
in the place where you call it, and only do the action if answer
returned 0.
Based on your earlier script, it would look something like this:
while tomcat_running && user_wants_to_stop_tomcat; do
echo "$tomcat_status_stopping"
kill $RUN
sleep 2
done
function tomcat_running() {
check_tomcat_status
[ -n "$RUN" ]
}
function user_wants_to_stop_tomcat() {
answer "WARNING: Tomcat still running. Kill it? "
}
function answer() {
while true; do
printf "$1"
read response
case $response in
[yY][eE][sS]|[yY])
return 0
;;
[nN][oO]|[nN])
return 1
;;
*)
printf "Please, enter Y(yes) or N(no)!\n"
;;
esac
done
}