LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   until loop (http://www.linuxquestions.org/questions/linux-newbie-8/until-loop-914973/)

ppatryk 11-22-2011 06:44 PM

until loop
 
hey, lookin for some advice, I am getting the user to input a letter, A or B, I only want the user to type in A or B, so how would I make my scirpt to check if thats the input and return and error if its something else until the user types in A or B.
many thanks

corp769 11-22-2011 06:49 PM

What programming language are you using? Or are you using bash scripting? You need to be more specific when asking questions like this.

ppatryk 11-22-2011 06:52 PM

hi, sorry, i am using bash scipting

AnanthaP 11-22-2011 07:03 PM

Pseudo code:
While not A and not B
Do
Get input
If input is not A and input is not B then
Message
Fi
Done

ppatryk 11-22-2011 07:07 PM

source code any chance plz?

corp769 11-22-2011 07:47 PM

How will you learn if we do it for you?

grail 11-22-2011 07:54 PM

Or at least tell us what part of the pseudo code you do not know how to do or understand?

basica 11-22-2011 08:14 PM

Do you have any familiarity with programming? As others mentioned you need to do a couple of things:

ask the question and input the data
check the input and if it doesn't match what is requested, repeat the question

Off the top of my head, it might look like this in BASH (will have to double check to be sure though..)

use while [$variable !=] to set the condition
use do to start the loop
use echo to ask the question
use read to store the input into a variable, i.e read variable
use done to finish it

corp769 11-22-2011 09:33 PM

Quote:

Originally Posted by basica (Post 4531142)
Do you have any familiarity with programming? As others mentioned you need to do a couple of things:

ask the question and input the data
check the input and if it doesn't match what is requested, repeat the question

Off the top of my head, it might look like this in BASH (will have to double check to be sure though..)

use while [$variable !=] to set the condition
use do to start the loop
use echo to ask the question
use read to store the input into a variable, i.e read variable
use done to finish it

He/She hit the nail on the head..... And besides the general questions and such, have you tried anything on your own as far as writing the script?

ppatryk 11-23-2011 10:48 AM

hey, thanks for the replies, I have wrote a bit of code, its doesnt use while, could someone help me finish it or advice what to do?

read answer
if [ $answer = "w" ]
then
find ~/folder.....
else
echo "Please type answer again"
until
[$answer = "w"]

grail 11-23-2011 11:41 AM

So what seems to be the problem with what you have so far?

It is not necessarily the most elegant but you seem to be on the right track.

ppatryk 11-23-2011 12:00 PM

im getting an syntax error - unexpected end of file, any help appreciated, thanks

corp769 11-23-2011 12:04 PM

Quote:

Originally Posted by ppatryk (Post 4531854)
im getting an syntax error - unexpected end of file, any help appreciated, thanks

Could you post the full code you have, using code tags?

ppatryk 11-23-2011 02:38 PM

yes of course -

echo "What file would you like - w-word, s-spreadsheet"
read answer
if [answer = "w" ]
then
find ~/folder...............
else
echo "Only type in w or s"
until
[$answer = "w"]

then same thing again but with the letter "s".

thanks

grail 11-24-2011 02:01 AM

Read http://tldp.org/LDP/abs/html/testconstructs.html for the correct format of an 'if' statement

Read http://tldp.org/LDP/abs/html/loops1.html for the correct format for an 'until' loop

Also, each time you reference a variable you need to place $ in front of it to get the data stored in it.

And we are guessing you know the find command would never work as shown.


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