简体   繁体   English

Expect Script:运行两个名称相似的文件

[英]Expect Script: Run two files with similar names

I am trying to automate the running of a program, using and expect script, but I am doing something wrong. 我试图自动运行程序,使用和期望脚本,但我做错了。 I have a bunch of files in my current directory that follow this naming convention: 我当前目录中有一堆文件遵循以下命名约定:

file_name-hereROI.txt
file_name-hereFR.txt
file1_name-hereROI.txt
file1_name-hereFR.txt
file_name1-hereROI.txt
file_name1-hereFR.txt
file_name-here1ROI.txt
file_name-here1FR.txt

What I would like to do is to run a program on every pair. 我想做的是在每一对上运行一个程序。 So if the string before "ROI" or "FR" is the same, it should be compared by water. 因此,如果“ROI”或“FR”之前的字符串相同,则应通过水进行比较。 I have written: 我已经写了:

  1 #/usr/bin/expect
  2 for f in *ROI.fasta
  3 do
  4   for f2 in *FR.fasta
  5   do
  6     fROI=$(echo $f | cut -d'ROI' -f 1)
  7     f2ROI=$(echo $f2 | cut -d'FR' -f 1)
  8     if [$fROI == $f2ROI]
  9     then
 10       spawn water f f2
 11       expect "Gap opening penalty [10.0]:"
 12       send "\n"
 13       expect "Gap extension penalty [0.5]:"
 14       send "\n"
 15       expect "Output alignment"
 16       send "\n"
 17       close
 18     fi
 19   done < ls
 20 done < ls

But I am not getting very far at all. 但我并没有走得太远。 I have this error: 我有这个错误:

$ expect water.sh 
wrong # args: should be "for start test next command"
    while executing
"for f in *ROI.fasta"
    (file "water.sh" line 2)

I think you're getting mixed up between bash and Tcl/Expect notation. 我认为你在bash和Tcl / Expect表示法之间混淆了。

Expect is an extension of Tcl, so if you're going to have your script interpreted by Expect you need to use Tcl's constructs. Expect是Tcl的扩展,所以如果你要用Expect解释你的脚本,你需要使用Tcl的结构。

For starters, you will need to use Tcl's "for" loop, documented here . 对于初学者,您需要使用Tcl的“for”循环, 在此处记录

Refer to the rest of the Tcl documentation for the other conditionals needed (if, while, etc). 有关所需的其他条件(if,while等),请参阅Tcl文档的其余部分。

foreach f [glob *ROI.txt] {
    set f2 "[string range $f 0 [expr {[string length $f] - 8}]]FR.txt"
    if {[file exists $f2]} {
        spawn water $f $f2
        # ...
    }
}

To avoid hardcoding the number "8" 为了避免硬编码“8”

set suffix "ROI.txt"
set new_suffix "FR.txt"
foreach f [glob *$suffix] {
    set prefix [regsub "$suffix\$" $f ""]
    # or
    set idx [string last $suffix $f]
    set prefix [string range $f 0 [incr idx -1]]

    set f2 $prefix$new_suffix
    # ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM