LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   string comparison in for loop (https://www.linuxquestions.org/questions/linux-software-2/string-comparison-in-for-loop-624187/)

harsshal 02-27-2008 05:56 AM

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?

Ygrex 02-27-2008 10:02 AM

try to use square brackets:
Code:

for((; [ "$msg" == "exit" ] ; ))
and either wrap $msg with qoutes or do not

H_TeXMeX_H 02-27-2008 11:46 AM

You can also use 'test' instead of the '[ ]', it's the same thing, but more readable, IMO. You can use whichever you want, tho.

harsshal 02-27-2008 10:44 PM

???
 
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?????

chrism01 02-28-2008 12:26 AM

For that situation, you should be using a while loop:
Code:

while [[ $msg != "exit" ]]
do
    do stuff
done


H_TeXMeX_H 02-28-2008 01:05 PM

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 ?

chrism01 02-28-2008 06:32 PM

Actually, that's a bash 'counted' for loop, c-style, see this page: http://learnlinux.tsf.org.za/courses...ting/ch08.html

harsshal 02-28-2008 11:59 PM

so cant i compare strings in FOR?

chrism01 02-29-2008 12:55 AM

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

H_TeXMeX_H 02-29-2008 03:11 AM

But what about the brackets '{', they should not be there in bash. It should be 'do' and 'done'.

makyo 02-29-2008 05:09 AM

Hi.
Quote:

Originally Posted by harsshal (Post 3073511)
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

harsshal 03-02-2008 08:58 PM

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???

makyo 03-02-2008 10:18 PM

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

harsshal 03-02-2008 11:15 PM

nop.

i have given what i want in first post.

chrism01 03-02-2008 11:20 PM

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.


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