Home > Enterprise >  expect: how to get rid of duplicated lines?
expect: how to get rid of duplicated lines?

Time:09-27

Say I have following program:

$ cat test1.c
#include <stdio.h>

int main() {
  int i;
  for (i = 0; i < 10;   i) {
    int n, res;
    res = scanf("%d", &n);
    if (res != 1) break;
    printf("%d\n", n * K);
  }
}

Compilation is simple:

$ gcc -DK=2 test1.c -o t2
$ gcc -DK=3 test1.c -o t3

I am then using expect to automate process: one program sends output to antother.

$ cat connect.tcl
#!/usr/bin/env expect

spawn ./t2
set idt2 $spawn_id

spawn ./t3
set idt3 $spawn_id

send -i $idt2 "1\r"
expect {
    -i $idt2 -re {(\d )\s*(\d )} {
        set q $expect_out(2,string)

        send -i $idt3 "$q\n"
        expect -i $idt3 -re {(\d )\s*(\d )}
        set a $expect_out(2,string)

        send -i $idt2 "$a\n"

        exp_continue
    }
    -i $idt2 eof {
        exp_continue
    }
    -i $idt3 eof {
    }
    timeout {
      puts "$expect_out(buffer)"
    }
}

Run it like this:

$ expect connect.tcl
1
2
2
6
6
12
12
36
36
72
72
...... etc .....

Everything is good but I am seeing everything 2 times.

What I want is:

$ expect connect.tcl
1
2
6
12
36
72
...... etc .....

I. e. I want first program to take input from second and vice versa and print only what is need to be printed.

I tried different versions of spawn -noecho, etc, but looks like nothing works. Also please note, that I am matching pair of numbers in my expect script: of course in desired solution regular expressions will slightly differ.

Will appreciate any ideas.

CodePudding user response:

By default expect logs interactions with spawned processes to stdout so I'll guess what you're seeing is one process outputting "12" and then you writing the "12" to the other. Try disabling that with log_user 0 and explicitly puts what you want to see instead.

  • Related