script to prompt yes/no to user.
hi,
I need to create a script to prompt yes/no(in text base, not show a dialog) to user.any one know how to do it? thanks! jimmy |
Yes. You'll need to construct a loop to test whether the response is y or n, of course.
Loops can be done like this: Code:
while true Code:
help read | less Code:
bash tutorial Hope this gets you started. Come back with more specific questions if you stumble. (We all stumble.) |
Yes/No reply
One way of solving the problem is to create a function in your script to validate Yes/No replies.
The function must be coded before it is called by the other coding in the script. eg. ___________________________________________ #1/bin/bash yn() { printf " Reply [y/n]: " read yn [[ "$yn" != y ]] && [[ "$yn" != n ]] && yn } # OTHER CODING # Now you pose a question # and then execute the yn function printf "do you want to continue, " yn [[ "$yn = n ]] && exit .......... .......... _______________________________________ Brief expalnation: The Yes/No function begins at yn() { and ends at } When ever you want to ask the user for a yes/no reply simply pose your quention with the printf statement and the execute the yn funtion. It will accept the reply and if it is not a n or a y it will continue to loop until a valid y or n reply is given. In the above case the script terminates if the reply is n The && is executed if the test is true. Oupa. Oupa. |
Wrong thread :(
|
All times are GMT -5. The time now is 10:26 PM. |