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? |
try to use square brackets:
Code:
for((; [ "$msg" == "exit" ] ; )) |
You can also use 'test' instead of the '[ ]', it's the same thing, but more readable, IMO. You can use whichever you want, tho.
|
???
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????? |
For that situation, you should be using a while loop:
Code:
while [[ $msg != "exit" ]] |
Hehe, I was tricked too, this is C code man:
Code:
for((; $msg=="exit"; )) Might I ask, what language are you trying to use here ? |
Actually, that's a bash 'counted' for loop, c-style, see this page: http://learnlinux.tsf.org.za/courses...ting/ch08.html
|
so cant i compare strings in FOR?
|
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... |
But what about the brackets '{', they should not be there in bash. It should be 'do' and 'done'.
|
Hi.
Quote:
Quote:
|
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??? |
Hi.
The tcl language has something like that: Code:
#!/usr/bin/env tclsh Code:
% ./tcl1 Quote:
|
nop.
i have given what i want in first post. |
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. |