如何从 expect 脚本将 "exec" 命令的结果记录到 log_file

How to log the "exec" command's result into log_file from an expect script

我想使用 log_file 命令捕获我的 expect 脚本的进度。在 expect shell 中发出以下命令后,所有内容都被捕获到日志文件中。

expect1.1> log_file my.log
expect1.2> exec timedatectl status
      Local time: St 2017-04-19 17:07:10 CEST
                ⋮ 
 RTC in local TZ: no

但是 运行 下面的脚本给我留下了空日志。

#!/usr/bin/expect -f
log_file mylog.log
exec timedatectl status

如何从 Expect 脚本捕获日志?

有一个similar question,不幸的是答案没有处理日志部分。

您可以捕获输出并将其显式发送到日志文件:

set output [exec echo hello world]
send_log $output

我认为它可以自动将 exec 结果记录到 log_file 的原因是(根据 man tclsh):

If invoked with no arguments then it runs interactively, reading Tcl commands from standard input and printing command results and error messages to standard output.

听起来像 log_file 会捕获所有写入 tty 的数据,但在脚本(非交互模式)中 exec 的结果不会自动打印到 tty。