1

I have a program that writes to stdout in a batch of text very quickly, but the output doesn't have a specific output that I can write an expect loop for. Example of stdout of my program:

[time:here] random text 1
[time:here] random text 2
[time:here] random text 3
[time:here] random text 4
[time:here] random text 5
[time:here] random text 6
[time:here] random text 7

Then it waits until I interact with it, and then it writes to stdout again with the same style of text:

[time:here] random text 8
[time:here] random text 9
[time:here] random text 10
[time:here] random text 11
[time:here] random text 12
[time:here] random text 13
[time:here] random text 14

The stdout is printed very fast, like within milliseconds, and then there's a wait until I interact with it. Once I interact stdout is written again very quick, and it waits. This repeats until I close the program.

Between the waits I want to send a command to write to another file what time I interacted with the program at (using echo or something similar).

Is there any way I can target the "wait" and do the echo command after every time there's a wait for my stdout? For example if there's a wait for > than 5 seconds then run the echo command, and wait for stdout to change again?

  • 2
    expect can handle a timeout event for both expect loops and when in interact mode, is there a reason those won't fly? – thrig Jul 31 '17 at 22:13
1

You could use a blocking read with a timeout to determine when output has stopped. For example, Bash's read supports a timeout parameter. The following script will write a single line if the output from STDIN stops for more than 2 seconds:

#!/bin/sh

while read -r firstline; do
    while read -r -t 2 line; do
        continue
    done
    echo "---- No data in 2 seconds ----"
done

The echo could be re-directed to a different log and the script can be modified to echo the data being read from standard in if desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.