Edit: I realized that the program I was trying to run had an infinite loop after scanning all my input. Then just prints out infinitely so it'll never read the EOF. It'll read the last input then go into an infinite loop
For example let's say the program is something like this.
printf("%c\n", getch());
while(1)
{
printf("%c\n", getch());
}
Is there a way to kill the program after reading in all my input? I want to be able to kill the program when it finishes redirecting the input file. (When the program gets the last character from the input file) Note: I cannot modify the program I'm running but I can modify the script that runs this program.
Right now I have this in my script to run the program. Basically this helps me combine/merge input + output together.
EDIT: Someone helped me with solving my earlier problem. I was suppose to add || break to this strace but I have a new problem which is in the edit at the top.
mkfifo strace.fifo
{
while read -d, trace; do
if [[ $trace = *"read(0" ]]; then
IFS= read -rn1 answer <&3 || break
echo "$answer" >> in
answer=${answer:-$'\n'}
printf %s "$answer" >&2
printf %s "$answer"
fi
done < strace.fifo 3< input | strace -o strace.fifo -e read stdbuf -o0 ./a.out &
} >> $fname.out 2>&1
So right now I have a really hacky way of trying to end the program by using sleep and killing the program. Also I'm checking the input used so far and comparing it with the input file. This is just a small example of what I'm doing my current script has multiple timer and if comparisons similar to the code below. However this way is not a good way because if the program isn't done reading the input file after whatever second I put it won't kill the program. In my real script I used multiple of these if statements and it works like 80% of the time but sometimes it won't do the right thing probably because of the timer and the fluctuation of the way the program runs.
sleep .05
awk 'BEGIN{ORS="";} NR==1{print; next;} /^[^ ]/ { print; next; } {print "\n";}' in > temp
diff -w -B temp input > /dev/null
# Check if input done yet
if [ $? -eq 0 ] ; then
# Check if program still running and input done
if [ "$(pidof a.out)" ] ; then
killall -15 a.out > /dev/null 2>&1
fi
fi
So I'm wondering is there a way to kill
the process in the strace
after input is done? Also I want to be able to keep input and output merged together.