如何在while循环中运行“file exists”函数(expect / tcl)作为条件?

时间:2017-05-10 20:09:55

标签: ssh while-loop expect file-exists not-exists

我知道这是与其他帖子类似的问题,但在尝试了代码的变体后,我无法得到我想要的结果。 expect脚本登录,提交集群作业并等待生成的文件被写入。我想定期检查结果文件,并在文件位于目录后继续我的expect脚本。当我运行以下代码时,即使我在另一个ssh会话中看到该文件,[file exists outgraph.json]也似乎永远不会等于1。我想我忽略了一些简单但却无法弄清楚为什么它在循环期间从未检测到文件导致期望脚本永远不会前进。

#Attempt 1
#Spawning and logging in
send "qsub -v QUERY=$query run\_query.pbs\r"
while {true} {
    after 2000
    if {[file exists outgraph.json] == 1} {
    break;
    }
    puts [file exists outgraph.json]
}
expect "$ "
send "rm -rf tmp1\r";
expect "$ "
send "rm input.fa\r";
expect "$ "
send "exit\r"
# moving file with sftp

#Attempt 2
# spawning and logging in
send "qsub -v QUERY=$query run\_query.pbs\r"
set fexist [file exists outgraph.json]
while {$fexist == 0} {
    after 2000;
    set fexist [file exists outgraph.json]
    puts $fexist
} 
expect "$ "
send "rm -rf tmp1\r";
expect "$ "
send "rm input.fa\r";
expect "$ "
send "exit\r"
# moving file with sftp

1 个答案:

答案 0 :(得分:0)

问题在于file exists。它会检查文件路径是否存在,仅在本地计算机上运行Expect脚本,不在远程目录中。

#This is a common approach for few known prompts
#If your device's prompt is missing here, then you can add the same.
set prompt "#|>|\\\$ $"; # We escaped the `$` symbol with backslash to match literal '$'

set filename outgraph.json
set cmd "(\[ -f $filename ] && echo PASS) || echo FAIL" 
send "$cmd\r"
expect {
    "\r\nPASS" { puts "File is available";exp_continue}
    "\r\nFAIL" { puts "File is not available";exp_continue}
    -re $prompt 
}