Zhro
Zhro

Reputation: 2614

Why is puts with '[' failing?

Can someone explain why this keeps failing?

set hostname ""

if {$hostname eq ""} {
   puts "Usage: host [-u username] [-p password] [-f]"
   exit 5
}

if {[string length $hostname] == 0} {
   puts "Usage: host [-u username] [-p password] [-f]"
   exit 5
}

 if {[string equal $hostname ""]} {
   puts "Usage: host [-u username] [-p password] [-f]"
   exit 5
}

I get the following error:

invalid command name "-u"
    while executing
"-u username"
    invoked from within
"if {$s eq ""} {puts "Usage: host [-u username] [-p password] [-f]"}"

The code seems to execute fine but I can't seem to use puts with a string that contains braces.

Upvotes: 2

Views: 176

Answers (2)

glenn jackman
glenn jackman

Reputation: 246764

Another approach is to use different quotes for the strings you print:

puts {Usage: host [-u username] [-p password] [-f]}

Braces act like the shell's single quotes: they prevent substitutions within the brace-quoted string.

You might also want to spend some time studying Tcl's 12 syntax rules:
http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

Upvotes: 7

user2579943
user2579943

Reputation:

Square braces inside a double-quoted string means evaluation. Try escaping the square braces with a backslash.

Upvotes: 8

Related Questions