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
You could use a
whileloop like this:Should preserve whitespace in lines. (The
-rinreadis to go into “raw” mode to prevent backslash interpretation.)