prompt_yes_no - prompt user for a yes/no reply
prompt_yes_no QUESTION [Yes/no]|[No/yes]
prompt_yes_no is a shell function that ask user for a yes/no reply.
It returns 0 if user answers yes (or y) or 1 if user answers no (or n).
prompt_yes_no is case independent; Yes, YES, YeS are valid replies for yes.
The reply is read from stdin; re-ask when the reply is invalid (i.e. reply is neither yes nor no).
Here is a simple example of how to use prompt_yes_no. It handles yes and no replies, whatever the case is used (upper or lower).
$ prompt_yes_no "Do you want to exit success" "[No/yes]"
Do you want to exit success [No/yes]? YES
$ echo $?
0
$ prompt_yes_no "Do you want to exit success" "[No/yes]"
Do you want to exit success [No/yes]? no
$ echo $?
1
It re-asks when replies are invalid (abc, def...)
$ prompt_yes_no "Do you want to exit success" "[No/yes]"
Do you want to exit success [No/yes]? abc
Invalid response!
Do you want to exit success [No/yes]? def
Invalid response!
Do you want to exit success [No/yes]?
...
It returns the default option in case of empty replies; when <Enter> is entered
$ prompt_yes_no "Do you want to exit success" "[No/yes]"
Do you want to exit success [No/yes]? <Enter>
$ echo $?
1
Or when stdin returns EOF (<Ctrl+D>)
$ prompt_yes_no "Do you want to exit success" "[No/yes]"
Do you want to exit success [No/yes]? <Ctrl+D>
$ echo $?
1
Here is a simple example of how to use it in a shell script
if prompt_yes_no "Are you sure you want to remove directory" "[No/yes]"
then
rm -rf directory-ask-to-be-removed/
fi
Written by Gaël PORTAY gael.portay@gmail.com
Copyright (c) 2015-2017 Gaël PORTAY
This program is free software: you can redistribute it and/or modify it under the terms of the MIT License.