Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Red Hat This forum is for the discussion of Red Hat Linux.


LinkBack Search this Thread
Old 04-10-2007, 04:32 PM   #1
Registered: Jun 2006
Posts: 132

Rep: Reputation: 15
script to prompt yes/no to user.

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?
Old 04-10-2007, 05:33 PM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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:

while true
  (some stuff to be done)

  if [ some condition ]

  (some more stuff to be done, if you want)
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:

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

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.)

Last edited by; 04-10-2007 at 05:35 PM.
Old 05-09-2007, 10:56 AM   #3
LQ Newbie
Registered: Apr 2007
Location: Welkom, South Africa
Distribution: RHL + Mandriva
Posts: 15

Rep: Reputation: 0
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.

yn() {
printf " Reply [y/n]: "
read yn
[[ "$yn" != y ]] && [[ "$yn" != n ]] && yn
# Now you pose a question
# and then execute the yn function

printf "do you want to continue, "
[[ "$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.

Old 11-20-2011, 07:43 PM   #4
LQ Newbie
Registered: Nov 2011
Posts: 4

Rep: Reputation: Disabled
Wrong thread

Last edited by Astroe; 11-20-2011 at 08:02 PM. Reason: Wrong Thread :( Sorry



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
add a user with password without prompt grendel-IT Linux - Software 7 09-27-2006 02:07 PM
command runs at prompt not in script newbie_m Linux - Newbie 2 01-20-2005 02:12 PM
How to pass arguments from $prompt for php script ukjairaj Linux - Software 4 06-25-2004 11:14 AM
Deleting a user account from prompt lozz1978 Linux - Newbie 8 01-15-2004 07:43 AM
User prompt Phaethar Red Hat 9 11-21-2003 01:41 PM

All times are GMT -5. The time now is 11:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration