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 -
bash -c './program < input1.txt & PID=$!; sleep 1; kill -INT $PID; '| diff -y output1.txt
. I have tested both solutions usingyes
as./program
.yes
diff -y output1.txt -
.