I have a process that prints to stdout at regular intervals (in seconds granularity), and I'd like to pipe the output of that process to a python program via stdin to process it. The problem I face is that I'd also like to take input from the user on how to process it before moving on. As a toy example,
The program (interval.sh
) that prints to stdout in regular intervals.:
#!/bin/bash
for i in {1..10}
do
echo $i
sleep 1s
done
Python program (test.py
) to process the input:
#!/usr/bin/env python3
import sys
for line in sys.stdin:
while True:
validate = input("Do you want to accept task {}? [y/n]\n".format(line))
if validate == 'y':
print("User accepted the input\n")
break
elif validate == 'n':
print("User rejected the input\n")
break
else:
print("Please enter a valid input")
I currently run the program like:
$ ./interval.sh | ./test.py
Do you want to accept task 1
? [y/n]
Input not supported
Do you want to accept task 1
? [y/n]
Input not supported
Do you want to accept task 1
? [y/n]
As you can see, the above program thinks that the input from the shellcode is the user's input. What I would like to do is:
$./interval.sh | ./test.py
Do you want to accept task 1? [y/n] y
The User accepted the input
Do you want to accept task 2? [y/n] y // Move to task 2 only when the user provides a valid input
The User accepted the input
Do you want to accept task 3? [y/n]
I see the problem because the input from the program and user are from stdin and hence difficult to differentiate. Furthermore, interval.sh
cannot be modified in my actual scenario. How else can I approach this problem?