Replace cat and grep with <

Hello
someone told me to use
OS=`awk '{print int($3)}' < /etc/redhat-release`

instead of
OS=cat /etc/redhat-release | `awk '{print int($3)}'`

any idea for the reason ?

Both versions will achieve the same result. The first is more efficient and should be the one of choice, as awk will open and read the input file directly. The second will make cat print the input file to its stdout which then is piped to and read by awk (stdin), spoiling a process creation (cat).

Sorry RudiC, but I have to disagree completely. (The first backquote in the 2nd example is misplaced.)

The first sequence sets OS to a list of the 3rd fields on every line of the file /etc/redhat-release .

The second sequence sets OS to cat and feeds the output produced by running the command /etc/redhat-release through the command found in the 3rd field typed into the script that is running this command sequence. And, when the pipeline finishes, the original state of OS is restored (unset if it hadn't been set, or set to the value it had before the above command if it had been set). Probably not what you wanted.

You could get the same results as the 1st command sequence much less efficiently using the command sequence:

OS=`cat /etc/redhat-release | awk '{print int($3)}'`

The 1st command sequence reads the data in /etc/redhat-release once, this reads the data in that file twice and writes it once. The 1st command sequence has to fork() and exec one process ( awk ); this has to fork() and exec two processes ( cat and awk ).

1 Like

Thanks, Don Cragun. I'm utterly afraid I need my eyeglasses reground and polished...

I almost missed it, too. I know I need to have my eyeglass prescription updated, but I think the problem here is that I initially saw what we all expected to see instead of what was there.

Hi.

A good reason to use $( pipeline ) as opposed to ` pipeline ` whenever available ... cheers, drl