Lots of good answers on this list. Thanks again to all. Have to say, if efficient code were the sole concern, we'd get there better with a compiled language. On 7/27/22 1:56 PM, Georg Pfuetzenreuter wrote:
FYI, both of these are UUOC.
while read line do # foo here done < $1
Best, Georg
On 7/27/22 19:35, kf wrote:
On 7/26/22 5:56 PM, Michael Hamilton wrote:
On Wednesday 27 July 2022, kf wrote:
#!/bin/bash
#How to declare the variable "count"??? count=0
cat $1 | while read line do count=$((count+1)) echo -n "$count " done
echo echo File $1 has $count lines.
As other have explained, the pipe receiver runs in it's own subprocess. If you were in a situation where you did really have to read from a pipe, you could also code it as follows:
#!/bin/bash cat $1 | { count=0 while read line do count=$((count+1)) echo -n "$count " done
echo echo File $1 has $count lines. }
Nice. Thanks. It's always good to have options.