我在awk中运行md5sum以找到相同的文件:
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
}'
b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
测试
test_cpy1
-rw-rw-r-- b1946ac92492d2347c6235b4d2611184
你可以看到有一个md5sum不正确......但如果我将两个cmd行序列更改为:
cmd2 | getline; sum2=$1
cmd1 | getline; sum1=$1
输出正确。我很困惑......
b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
test
test_cpy1
b1946ac92492d2347c6235b4d2611184 b1946ac92492d2347c6235b4d2611184
test_cpy1
test_cpy2
答案 0 :(得分:2)
好的,我发现我需要关闭cmd:
cmd1 | getline; print name1,$0;sum1=$1;
close(cmd1)
cmd2 | getline; print name2,$0;sum2=$1;
close(cmd2)
来自:http://www.gnu.org/software/gawk/manual/html_node/Getline_002fPipe.html#Getline_002fPipe
一次只能打开一个管道。如果要打开另一个管道,则必须执行
close("command");