LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   script to prompt yes/no to user. (https://www.linuxquestions.org/questions/red-hat-31/script-to-prompt-yes-no-to-user-544937/)

jimmyjiang 04-10-2007 04:32 PM

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

wjevans_7d1@yahoo.co 04-10-2007 05:33 PM

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
do
  (some stuff to be done)

  if [ some condition ]
  then
    break
  fi

  (some more stuff to be done, if you want)
done

You'll need to use the read statement to input a line from the keyboard. For information on that, do this at the command line:

Code:

help read | less
For general information on shell scripts, google this:

Code:

bash tutorial
... and you'll get oodles of info on writing bash shell scripts.

Hope this gets you started. Come back with more specific questions if you stumble. (We all stumble.)

Oupa 05-09-2007 10:56 AM

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.

Astroe 11-20-2011 07:43 PM

Wrong thread :(


All times are GMT -5. The time now is 10:26 PM.