I've tested the while read ...
code in the question with input lines of 8MB and it works fine. I assume your issue is that it is too slow.
If you can use Perl then this code shows one way to do it:
perl -Mautodie -nle "open P, '| ./process.sh'; print P; close P" input.txt
- The
-Mautodie
causes the code to fail with error messages if any file or pipe operations fail. Although the autodie
module has been a core module in Perl for over a decade, it may not be available in some Perl installations. Some are very old. Others (for unknown reasons) don't include all core modules. The code will work if you remove -Mautodie
, but it may fail silently if something goes wrong.
If Python is an option, this code may be of use:
IFS= read -r -d '' python_code <<'_END_PYTHON_CODE_'
import sys
from subprocess import Popen, PIPE
for line in sys.stdin:
with Popen(sys.argv[1:], stdin=PIPE, text=True) as p:
p.stdin.write(line)
_END_PYTHON_CODE_
python -c "$python_code" ./process.sh <input.txt
- I don't have much experience of Python, so the code may not be good. It runs slightly slower than the Perl code in my testing.