ActiveState Community

if statement

Posted by dave_m on 2008-09-10 04:21

I have a program telneted from a windows PC to a UX box, I need to
use an "if" statement. this as far as I know should work.

set A 5

if {A==5} {send "ls\r"}
else
{send "cd ..\r"}

I get an invalid command failure. doesn't understand
"else"

I've ried it 50 ways... Any help would be appreciated

Thanks,

Dave

beernut | Tue, 2008-09-16 08:57

Dave,
Two problems here. You need an $A in your if statement, not a "A." The letter A will never match the number 5. Also, you need some spaces between your "{"'s

Try this:
if { $A == 5 } { 
    send "ls\r"
} else {
    send "cd ..\r"
}

umb365 | Sun, 2009-02-22 09:04

Dave is right. TCL needs blanks char to sperate arguments.
And, pay attention to the location of "{".
Remember, leave the "{" at the end of the line.

Explain:
the actually "if" statement should be:
if { $A == 5 } { send "ls\r" } else { send "cd ..\r" }
But, as you see, this statement is not suit for human reading.
so we use some linefeed to make it looks like hi-level code style.

TCL interpreter recogniszes a statement by linefeed & ";".
Your code broke the "if" statement into 3 statments:
1: if {A==5} {send "ls\r"}
2: else
3: {send "cd ..\r"}

Read the manual "grouping" part for detail.

My English is not good, hope you can understand what I said. :P