I am having a bash script which is something like following,
cat filename | while read line
do
read input;
echo $input;
done
but this is clearly not giving me the right output as when I do read in the while loop it tries to read from the file filename because of the possible I/O redirection.
Any other way of doing the same?
Try to change the loop like this:
Unit test:
Read from the controlling terminal device:
more info: http://compgroups.net/comp.unix.shell/Fixing-stdin-inside-a-redirected-loop
You can redirect the regular stdin through unit 3 to keep the get it inside the pipeline:
BTW, if you really are using
cat
this way, replace it with a redirect and things become even easier:Or, you can swap stdin and unit 3 in that version -- read the file with unit 3, and just leave stdin alone:
It looks like you read twice, the read inside the while loop is not needed. Also, you don't need to invoke the cat command: