Ask Question
- ask_question
Bash CLI version of ask_question
- Arguments:
$1
- The question[
$2
] - Default answer (when user presses enter only)- Output:
Returns
0
for yes,1
for no
See also
- ask_question
Pretty version of ask Y/N question with optional default value
- Arguments:
$1
- The question$2
- Variable to store answer in: 1 for yes, 0 for no[
$3
] - Default answer (when user presses enter only)[
!2
] - If the value of the variables named by $2 is set, then this will override the answer for the question asked. This is a good way to automate calling a script/function that contains ask_question and skip prompting the user for an answer.- Output:
Stores
1
for yes, and0
for no in the variable specified by$2
Example
source "${VSI_COMMON_DIR}/linux/ask_question"
ask_question "Continue?" y
ask_question "Would you like to play a game?" play y
ans=y
ask_question "This question will not wait, but answer yes for you." ans n
Note
Only the first letter of the response matters. Accepted values are yYnN (and blank if a default answer is given), else the question is asked again until an appropriate response is given.
Warning
If the same variable is used to store the answer for multiple ask_question
calls, then the result from the first call will act as the override. Luckily it is a 1
or 0
, so it won’t give a valid answer. But the best practice is to use different variable names for consecutive ask_question’s