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

时间:2015-10-28 12:15:34

标签: tcl expect logfile

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

每次我尝试将变量“$ time”附加到它返回的文件名时,这就是它开始变得挑剔的地方: 无法打开“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}“'string和'log_file”$ filename“'它可以正常工作,但它会将新信息附加到现有文件我每次运行脚本时都想要一个新文件。

如果我使用'append newfile'$ {a} $ {arg1} $ {suffix}“'string和'log_file”$ newfile“'它将无效并返回已经引用的错误。

希望你们能帮助我,并提前感谢任何支持。

2 个答案:

答案 0 :(得分:0)

您正在创建包含/的时间戳。

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

答案 1 :(得分:0)

在弄清楚日期格式弄乱了我的代码之后,我开始玩特殊字符并让它像这样工作:

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

这不是理想的格式,但至少我按照我的意图使它工作。