Lory_yang
Lory_yang

Reputation: 363

run md5sum commands in awk

I run md5sum in awk to find same file:

    ls -lS | awk 'BEGIN {
    getline;getline;
    name1=$9;size1=$5
}
{
    name2=$9;size2=$5;
    if (size1==size2) {
        cmd1="md5sum "name1
        cmd2="md5sum "name2
        cmd1 | getline; sum1=$1
        cmd2 | getline; sum2=$1
        print sum1,sum2
        if (sum1 == sum2) {
            print name1;print name2
        }
    };
    name1=name2;size1=size2
}'
  1. the output of my shell is :

b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
test
test_cpy1
-rw-rw-r-- b1946ac92492d2347c6235b4d2611184

You can see there's one md5sum is not right...but if I change the two cmd lines sequence to:

cmd2 | getline; sum2=$1 
cmd1 | getline; sum1=$1

The output is right. I'm confused...

b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
test
test_cpy1
b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
test_cpy1
test_cpy2

Upvotes: 3

Views: 1849

Answers (1)

Lory_yang
Lory_yang

Reputation: 363

Ok, I find that I need to close the cmd:

cmd1 | getline; print name1,$0;sum1=$1;
close(cmd1)
cmd2 | getline; print name2,$0;sum2=$1;
close(cmd2)

From: http://www.gnu.org/software/gawk/manual/html_node/Getline_002fPipe.html#Getline_002fPipe

Only one pipe can be open at a time. If you want to open another pipe, you must execute

close("command");

Upvotes: 3

Related Questions