我知道以前曾经问过这个问题。
但是请听一次......
我正在使用Cisco路由器(IOS)。
我必须编写一个执行某些命令并将其输出重定向到文件的脚本。但是我没有为每个命令使用tee
,而是打开一个文件,然后运行命令,其输出将被重定向到文件,然后关闭文件。
甚至重定向操作符>
也无效或以下答案:
答案 0 :(得分:2)
该问题中的解决方案无法为您提供信息这一事实:您遇到的真正的问题是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
您会发现它会产生 lot 输出。减少输出是一件非常有用的事情,所以这会将它简化为立即执行的命令(而不是他们调用的所有命令):
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
你可能仍会得到比你想要的更多的输出......