0

for example, there is a C program test.c:

#include<stdio.h>
int main(){
int aa;
scanf("%d",&aa);
return 0;
}

I want to write a bash script that can automatically execute this program. And what's more, if the input in the C program (aa) needs to be delayed for a few seconds after the program is called, how should the script be written?

The problem is that I don't know how to set the delay.

./test<<EOF
8
<<EOF
  • Actually this doesn't help....the problem is how to delay the input in the called program, the actually scenario is that I need to collect data in an experiment, so the called program needs to be delayed for a few seconds. – Muller Thomas Oct 27 '22 at 06:37
  • In this case, you have to "remote control" the program. Instead of bash, write a script in _tcl/expect_[https://www.tcl.tk/man/expect5.31/expect.1.html] which starts the program using `spawn`, then waits, and then sends the input. There is also a variant of _expect_ for shells, called `sexpect`[https://github.com/clarkwang/sexpect]. Yet another possibility is to use `telnet` together with [this](https://www.shellscript.sh/expect.html) script. – user1934428 Oct 27 '22 at 06:53

1 Answers1

2

If I understand correctly, you want to start the program with nothing in its input stream, and then a couple of seconds later provide some input? If that's right, you can pipe its input from what's essentially a small script that delays a couple of seconds, then sends data. Something like this:

{ sleep 2; echo "8"; } | ./test
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151