LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


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

Rep: Reputation: 15
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
 
Old 04-10-2007, 05:33 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

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

Last edited by wjevans_7d1@yahoo.co; 04-10-2007 at 05:35 PM.
 
Old 05-09-2007, 10:56 AM   #3
Oupa
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.
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.
 
Old 11-20-2011, 07:43 PM   #4
Astroe
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
 
  


Reply

Tags
function



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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 07:22 PM.

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