每次运行 TCL 脚本时创建一个新文件

Create a new file every time a TCL script runs

我是 TCL 的新手,有一些东西需要自动化,我需要我的代码在登录过程后记录所有命令和结果。 我的主要问题是我每次 运行 脚本时都需要创建一个不同的日志文件,我发现的一种方法是 "append" 文件名的唯一 "timestamp"。

这是它开始变得挑剔的地方,你看,每次我尝试将变量“$time”附加到文件名后 returns: 无法打开“15-10-28/11:57:10--xxx.xxxx.xxxx.txt”:没有这样的文件或目录 在执行时 "log_file "$newfile" " (文件"ssh-test.tcl"第31行)

我的代码如下:

set user [lrange $argv 0 0]
set password [lrange $argv 1 1] 
set ipaddr [lrange $argv 2 2]   
set arg1 [lrange $argv 3 3] 
set systemTime [clock seconds]
set time [clock format $systemTime -format %y-%m-%d/%H:%M:%S--]
set a "ssh"
set suffix ".txt"
append newfile "${a}${arg1}${suffix}"

set timeout -1   
# now connect to remote UNIX box (ipaddr) with given script to execute

spawn ssh $user@$ipaddr
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password 
send -- "$password\r"
log_file "$newfile" ;
expect "*#"
send -- "\r"
send_user "This is the $argv0 Script"
send -- "scm $arg1\r"
expect "*#"
send -- "exit\r"
expect eof

如果我使用 'set filename "${a}${arg1}${suffix}"' 字符串和 'log_file "$filename"' 它工作得很好,但它会将新信息附加到已经存在的文件中,我每次 运行 都想要一个新文件脚本。

如果我使用 'append newfile "${a}${arg1}${suffix}"' 字符串和 'log_file "$newfile"' 它将不起作用并且 return 错误已经提到。

希望大家能帮帮我,在此先感谢大家的支持。

您正在创建带有 / 的时间戳。

set time [clock format $systemTime -format %y-%m-%d/%H:%M:%S--]

将其附加到变量 newfile 时,它将变为 15-10-28/11:57:10--xxx.xxxx.xxxx.txt

Expect 会认为有一个名为 15-10-28 的文件夹可用,我必须在该文件夹下创建文件 11:57:10--xxx.xxxx.xxxx.txt。由于该文件夹不可用,您收到的错误消息为 no such file or directory

在发现日期格式弄乱了我的代码后,我开始尝试使用特殊字符并让它像这样工作:

set time [clock format $systemTime -format %a_%b_%d_%Y@%H'%M'%S]

这不是我想要的格式,但至少我让它按预期工作了。