如何在while循环期间运行“文件存在”函数(expect/tcl)作为条件?
Posted
技术标签:
【中文标题】如何在while循环期间运行“文件存在”函数(expect/tcl)作为条件?【英文标题】:How to run "file exist" function (expect/tcl) as condition during a while loop? 【发布时间】:2017-05-10 20:09:55 【问题描述】:我知道这是与其他帖子类似的问题,但在尝试了他们的代码变体后,我无法得到我想要的结果。期望脚本登录,提交集群作业并等待写入结果文件。我想定期检查结果文件,并在文件位于目录后继续执行我的期望脚本。当我运行以下代码时,[file exists outgraph.json]
似乎永远不会等于 1
,即使我在另一个 ssh 会话中看到该文件也是如此。我想我忽略了一些简单的事情,但无法弄清楚为什么它在循环期间从未检测到文件,导致期望脚本永远不会前进。
#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】:问题是因为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
【讨论】:
嘿 Dinesh,我在上面的代码周围放了一个循环,它正是我需要的。再次感谢!以上是关于如何在while循环期间运行“文件存在”函数(expect/tcl)作为条件?的主要内容,如果未能解决你的问题,请参考以下文章