I've been trying to write a simple bash script, but I cannot get it to work.
I want to:
- Start a program, feeding it input from a text file (
./prog < input1.txt) - Wait a short amount of time and the kill it as if it was served a keyboard interrupt (
& PID=$!; sleep 1; kill -INT $PID) source - Find the differences between the program's output and a text file (
| diff -y output1.txt -) source
Here's what I have now, putting the previous steps together:
./program < input1.txt & PID=$!; sleep 1; kill -INT $PID | diff -y output1.txt -
This version always reports that the first command has had no output, since the PID line is shadowing it. If I add even a file redirect after the program name, the kill command stops working since it is now pointing at the redirect.
Edit: I am on Ubuntu 16.04; the output of bash --version is GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
{ ./program < input1.txt & PID=$!; sleep 1; kill -INT $PID; }| diff -y output1.txt -– Weijun Zhou Mar 17 '19 at 03:42bash -c './program < input1.txt & PID=$!; sleep 1; kill -INT $PID; '| diff -y output1.txt. I have tested both solutions usingyesas./program. – Weijun Zhou Mar 17 '19 at 04:38yes– Alex Vermillion Mar 17 '19 at 04:43diff -y output1.txt -. – Weijun Zhou Mar 17 '19 at 04:45yesis being recorded in some way? – Alex Vermillion Mar 17 '19 at 05:00diff.catworks fine. – Weijun Zhou Mar 17 '19 at 05:03yesand it worked (more specifically,base64 /dev/urandom). I don't know how many data./programis generating. You may also try turning off buffering withstdbuf. – Weijun Zhou Mar 17 '19 at 05:36<()hours ago, also failed withyes. – Weijun Zhou Mar 17 '19 at 08:32