LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-27-2008, 05:56 AM   #1
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Rep: Reputation: 15
string comparison in for loop


hi

why cant we set our exiting condition as a string comparison in for loop?

The code is
for((; $msg=="exit"; ))
{
READ $msg;
}

IDEALLY the loop should end if enterd string is "exit",but it doen't.i tried all possible syntax too.
basically i read somewhere ((...)) are used only for integers.is it causing this?

Last edited by harsshal; 02-27-2008 at 06:02 AM.
 
Old 02-27-2008, 10:02 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
try to use square brackets:
Code:
for((; [ "$msg" == "exit" ] ; ))
and either wrap $msg with qoutes or do not
 
Old 02-27-2008, 11:46 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You can also use 'test' instead of the '[ ]', it's the same thing, but more readable, IMO. You can use whichever you want, tho.
 
Old 02-27-2008, 10:44 PM   #4
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Original Poster
Rep: Reputation: 15
???

after giving [..] i got this error

./temp.sh: line 3: ((: [ asd == exit ] : syntax error: operand expected (error token is "[ asd == exit ] ")

and after giving test [ .. ] no matter what is the input it doesnt go inside the loop

now?????
 
Old 02-28-2008, 12:26 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For that situation, you should be using a while loop:
Code:
while [[ $msg != "exit" ]]
do
    do stuff
done
 
Old 02-28-2008, 01:05 PM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Hehe, I was tricked too, this is C code man:
Code:
for((; $msg=="exit"; ))
{
READ $msg;
}
with a bit of bash sprinkled in, to make a nice cake.

Might I ask, what language are you trying to use here ?
 
Old 02-28-2008, 06:32 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, that's a bash 'counted' for loop, c-style, see this page: http://learnlinux.tsf.org.za/courses...ting/ch08.html
 
Old 02-28-2008, 11:59 PM   #8
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Original Poster
Rep: Reputation: 15
so cant i compare strings in FOR?
 
Old 02-29-2008, 12:55 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I suspect from the page I linked to that you can't (it's a 'counted loop'), but as I said, it's not the right construct for what you are trying to do anyway.
You really want my first post...
 
Old 02-29-2008, 03:11 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
But what about the brackets '{', they should not be there in bash. It should be 'do' and 'done'.
 
Old 02-29-2008, 05:09 AM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by harsshal View Post
so cant i compare strings in FOR?
Quote:
for (( expr1 ; expr2 ; expr3 )) ; do list ; done

First, the arithmetic expression expr1 is evaluated according to
the rules described below under ARITHMETIC EVALUATION. The
arithmetic expression expr2 is then evaluated repeatedly until
it evaluates to zero. Each time expr2 evaluates to a non-zero
value, list is executed and the arithmetic expression expr3 is
evaluated.

-- excerpt from man bash
The man pages are a good source for answers to questions of fact such as this ... cheers, makyo
 
Old 03-02-2008, 08:58 PM   #12
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Original Poster
Rep: Reputation: 15
there is another way for FOR loops

for{..}{..}{..}
{

}

(If we put integer comparison in place of string it works fine.means 'do' and 'done are not required.)

but even in this string comparison doesn't work.

ofcourse as chrism01 said we can do it in while,but whats the problem in for???

Last edited by harsshal; 03-02-2008 at 09:04 PM.
 
Old 03-02-2008, 10:18 PM   #13
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

The tcl language has something like that:
Code:
#!/usr/bin/env tclsh

# @(#) tcl1     Demonstrate tclsh for loop.

set message { Hello, world from tclsh.}
puts stdout $message

for { set x 0 } { $x < 10 } { incr x } {
  puts  " x is $x"
}
Producing:
Code:
% ./tcl1
 Hello, world from tclsh.
 x is 0
 x is 1
 x is 2
 x is 3
 x is 4
 x is 5
 x is 6
 x is 7
 x is 8
 x is 9
Is this what you are thinking of?
Quote:
for start test next body

For is a looping command, similar in structure to the C for statement. The start, next, and body arguments must be Tcl command strings, and test is an expression string.

Excerpt from http://tmml.sourceforge.net/doc/tcl/for.html
Best wishes ... cheers, makyo

Last edited by makyo; 03-02-2008 at 10:20 PM.
 
Old 03-02-2008, 11:15 PM   #14
harsshal
Member
 
Registered: Jul 2006
Location: New York, NY
Distribution: redhat,ubuntu,RHEL,fedora,centOS
Posts: 105

Original Poster
Rep: Reputation: 15
nop.

i have given what i want in first post.
 
Old 03-02-2008, 11:20 PM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you read makyo's post (num 11), he quotes the bash manual where it tells you that that style of for loop uses ARITHMETIC expressions.
 
  


Reply



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
string comparison in bash davimint Programming 4 01-23-2008 06:36 PM
String Comparison gjagadish Programming 6 11-06-2007 03:34 AM
String comparison in while [ ... ] condition hiwa Linux - Software 3 05-27-2007 02:05 AM
bash string comparison noir911 Programming 1 01-25-2006 06:37 PM
Perl String comparison Xris718 Linux - General 5 04-01-2005 01:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:25 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