Save terminal output to variable in expect/tcl

5,952

To automate the iteration, you can do this:

#!/usr/bin/expect

spawn ./offset_fitm_exp

set range   1.6
set azimuth 1.0

while {true} {
    expect "enter minimum cross-correlation threshold:"
    send "0.15\r" 

    expect "enter the range and azimuth error thresholds:" 
    send "$range $azimuth\r"

    expect -re {range: ([0-9.]+)   azimuth: ([0-9.]+} {
        set range   $expect_out(1,string)
        set azimuth $expect_out(2,string)
    }

    expect "set new error bounds? (0: no, 1: yes):" { 
        if {$range > 0.02 || $azimuth > 0.02} {
            send "1\r"
        } else {
            send "0\r" 
            break
        }
    }
}

interact
Share:
5,952

Related videos on Youtube

Piotr De
Author by

Piotr De

Updated on September 18, 2022

Comments

  • Piotr De
    Piotr De over 1 year

    Hej all, I have to use an interactive program and I want to automatize it with expect. My experiences in Linux and scripting is not the best. So this is the program output:

    enter minimum cross-correlation threshold: 0.15
    enter the range and azimuth error thresholds: 1.6 1.0
    
    range, azimuth error thresholds: 1.60000     1.00000
    cross-correlation threshold: 0.15000
    *
    *
    *
    model fit std. dev. (samples) range: 0.5298   azimuth: 0.4166
    set new error bounds? (0: no, 1: yes): 0
    

    The first two lines and the last one are interactive. So with expect and send it was not a problem to enter the thresholds for one time. But I want to iterate this process. Therefore I have to save the results "range: 0.5298 azimuth: 0.4166" into two variables and then run again until I get good results e.g. "range: 0.02 azimuth: 0.02".

    Anyone have an idea how I can store the results, compare them, and iterate the process?

    That works:

    #!/usr/bin/expect
    spawn ./offset_fitm_exp
    expect "enter minimum cross-correlation threshold:" { send "0.15\r" }
    expect "enter the range and azimuth error thresholds:" { send "1.6 1.0\r" }
    expect "set new error bounds? (0: no, 1: yes):" { send "0\r" }
    interact
    

    Thanks for your help! Bjoern

    • kos
      kos over 8 years
      Can you add what you have already? The answer will probably need to be integrated deeply with it.
    • j0h
      j0h over 8 years
      is the zero after the "):" a default output, or did you put that in?
    • Piotr De
      Piotr De over 8 years
      I added the code now. I put it in
    • j0h
      j0h over 8 years
      wait... are you hitting enter at each one of those lines? I thought that was program output. one potential problem i see is that ":" is used in places besides the prompt.
    • Piotr De
      Piotr De over 8 years
      No, not at each line. I only hit enter after I put the values in (lines 1,2 and 10)
    • j0h
      j0h over 8 years
      Do you have source code for the program? (thinking that might be significantly easier to make the automation happen)
    • Piotr De
      Piotr De over 8 years
      unfortunately not
    • fiatux
      fiatux over 8 years
      This question is more relevant for Stack Overflow, not askubuntu