定期的に (秒単位で) stdout に出力するプロセスがあり、そのプロセスの出力を stdin 経由で python プログラムにパイプして処理したいと考えています。私が直面している問題は、先に進む前に処理方法についてユーザーからの入力も取りたいということです。おもちゃの例として、
interval.sh定期的に stdout に出力するプログラム ( ):
#!/bin/bash
for i in {1..10}
do
echo $i
sleep 1s
done
入力を処理するPython プログラム ( test.py):
#!/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")
現在、次のようにプログラムを実行しています。
$ ./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]
ご覧のとおり、上記のプログラムは、シェルコードからの入力がユーザーの入力であると考えています。私がやりたいことは次のとおりです。
$./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]
プログラムとユーザーからの入力は標準入力からのものであり、区別が難しいため、問題が発生します。さらに、interval.sh実際のシナリオでは変更できません。他にどのようにこの問題に取り組むことができますか?