LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2011, 05:44 PM   #1
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Rep: Reputation: Disabled
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
 
Old 11-22-2011, 05:49 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
What programming language are you using? Or are you using bash scripting? You need to be more specific when asking questions like this.
 
Old 11-22-2011, 05:52 PM   #3
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
hi, sorry, i am using bash scipting
 
Old 11-22-2011, 06:03 PM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 11-22-2011, 06:07 PM   #5
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
source code any chance plz?
 
Old 11-22-2011, 06:47 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
How will you learn if we do it for you?
 
Old 11-22-2011, 06:54 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Or at least tell us what part of the pseudo code you do not know how to do or understand?
 
Old 11-22-2011, 07:14 PM   #8
basica
Member
 
Registered: Nov 2011
Location: Australia
Distribution: Arch, LFS
Posts: 171

Rep: Reputation: 38
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
 
Old 11-22-2011, 08:33 PM   #9
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by basica View Post
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?
 
Old 11-23-2011, 09:48 AM   #10
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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"]
 
Old 11-23-2011, 10:41 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
 
Old 11-23-2011, 11:00 AM   #12
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
im getting an syntax error - unexpected end of file, any help appreciated, thanks
 
Old 11-23-2011, 11:04 AM   #13
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by ppatryk View Post
im getting an syntax error - unexpected end of file, any help appreciated, thanks
Could you post the full code you have, using code tags?
 
Old 11-23-2011, 01:38 PM   #14
ppatryk
LQ Newbie
 
Registered: Nov 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-24-2011, 01:01 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

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


Reply

Tags
bash scripting, conditional, loop



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:57 AM.

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