2
votes

I´m pretty new in TCL. I made a script with the following code:

if {[file exists /proc/cpuinfo]} {
  exec grep "model name" /proc/cpuinfo
  exec grep "cpu MHz"    /proc/cpuinfo
}

puts "Hostname : [info hostname]"

For some reason, the exec command is not executed. Even if I take out the brackets, the command is not working. Besides, if I execute command by command in tclsh, it works correctly.

tclsh
%exec grep "model name" /proc/cpuinfo
model name :Intel(R) Xeon(R) CPU    @2.53GHz
%exec grep "cpu MHz" /proc/cpuinfo
cpu MHz    :2533.423
1
You're not doing anything with what exec returns... - Shawn

1 Answers

2
votes

I'll expand a little on what Shawn rightly wrote, since this behavior is quite unexpected for someone new to tclsh (like me), although it is explained by these words in the exec manual page:

If standard output has not been redirected then the exec command returns the standard output from the last command …

It notably says returns, which doesn't mean that it's printed, but rather it's discarded if we don't do something with the output, like passing it to puts like you did with [info hostname]:

  puts [exec grep "model name" /proc/cpuinfo]
  puts [exec grep "cpu MHz"    /proc/cpuinfo]

We get away without puts in interactive mode because the command result there is printed automatically.