Best way to pipe a program that reads only one line of input
bash, linux
Solution
You could use a `while` loop like this:
#! /bin/bash
IFS=$'\n'
while read -r line ; do
echo "$line" | cmd1
done < <(cmd2)
Should preserve whitespace in lines. (The `-r` in `read` is to go into "raw" mode to prevent backslash interpretation.)
Problem
Suppose I have a command cmd1 that reads one line of input from standard input and produces one line of output. I also have another command cmd2 which produces multiple lines of output. How do I pipe these two commands in linux so that cmd1 is executed for each line produced by cmd2? If I simply do: ``` # cmd2 | cmd1 ``` cmd1 will take only the first line of output from cmd2, produce one line of output and then close. I know I can use an interpreter like perl to do the job, but I wonder if there's a clean way to do it using bash script only. Thanks