【问题标题】:Redirect stdout to a file in tcl将标准输出重定向到 tcl 中的文件
【发布时间】:2014-06-16 06:40:50
【问题描述】:

我知道以前有人问过这个问题。

但是听我说一次..

我正在开发 Cisco 路由器 (IOS)。

我必须编写一个脚本来执行一些命令并将它们的输出重定向到一个文件。但是我不想对每个命令都使用tee,而是想打开一个文件然后运行命令,其输出将被重定向到文件然后关闭文件。

甚至重定向运算符> 也不起作用或以下答案:

How can I redirect stdout into a file in tcl

【问题讨论】:

    标签: redirect tcl io-redirection tee cisco-ios


    【解决方案1】:

    该问题中的解决方案对您不起作用这一事实提供了信息:您遇到的真正问题是 Tcl 命令通常不会写入 stdout 无论如何(除了puts,以它为主要工作,parray,这是一个在内部使用puts 的过程)。你习惯的“写信给stdout”只是交互式Tcl shell的一个特性。

    要在运行脚本时捕获所有命令的结果,您需要这样的包装器:

    trace add execution source leavestep log_result
    proc log_result {cmd code result op} {
        puts stdout $result
    }
    source theRealScript.tcl
    

    您会发现它会产生 很多 的输出。减少输出是一件相当有用的事情,因此这会将其减少为仅立即执行的命令(而不是它们调用的所有命令):

    trace add execution source enterstep log_enter
    trace add execution source leavestep log_result
    proc log_enter {args} {
        global log_depth
        incr log_depth
    }
    proc log_result {cmd code result op} {
        global log_depth
        if {[incr log_depth -1] < 1 && $result ne ""} {
            puts stdout $result
        }
    }
    source theRealScript.tcl
    

    你可能仍然会得到比你想要的更多的输出......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多