user3431399
user3431399

Reputation: 499

How to echo message to a file in TCL

I am trying to log down the variable_A value by redirect the echo to variable_A.log as below:

echo $variable_A >> variable_A.log

But instead of printing the value to my log file, it actually treat "$varaible_A >> variable_A.log" as string and printed on the prompt.

Upvotes: 1

Views: 13112

Answers (2)

glenn jackman
glenn jackman

Reputation: 246817

This is less efficient, but easy to grok:

exec echo $message >> filename

In an interactive tclsh session, it will work without the exec due to the way the defalt unknown procedure is defined.

Upvotes: 2

Donal Fellows
Donal Fellows

Reputation: 137577

To append a value to a file, you need to open the file in the right mode and use the two-argument form of puts (the one-argument form is defaulting to writing to stdout). Let's make a trivial procedure to do it:

proc appendToFile {message >> filename} {
    set f [open $filename "a"];   # The “a” is the key part
    puts $f $message
    close $f
}

Now we can use that dead easy:

appendToFile $variable_a >> variable_A.log

The >> in this case is just syntactic sugar. It has no value at all other than to make what you're doing easier to read. (>> is a legal — but quite strange — variable name in Tcl.)

Upvotes: 2

Related Questions