我是 TCL 的新人。我用以下代码制作了一个脚本:
if {[file exists /proc/cpuinfo]} {
exec grep "model name" /proc/cpuinfo
exec grep "cpu MHz" /proc/cpuinfo
}
puts "Hostname : [info hostname]"
由于某种原因,exec 命令没有被执行。即使我去掉括号,该命令也不起作用。 此外,如果我在 tclsh 中逐个命令执行命令,它可以正常工作。
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
最佳答案
我将稍微扩展一下 Shawn 正确写的内容,因为这种行为对于 tclsh
的新手(比如我)来说是非常意外的,尽管 exec
manual page 中的这些词对此进行了解释。 :
If standard output has not been redirected then the exec command returns the standard output from the last command …
它特别提到returns,这并不意味着它被打印,而是如果我们不对输出做一些事情,比如将它传递给puts
,它就会被丢弃> 就像您对 [info hostname]
所做的那样:
puts [exec grep "model name" /proc/cpuinfo]
puts [exec grep "cpu MHz" /proc/cpuinfo]
我们无需在交互模式下使用puts
,因为命令结果会自动打印。
关于linux - 为什么 exec 命令在 TCL 上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66730351/