Bash 循环比较文件 [英] Bash loop to compare files
本文介绍了Bash 循环比较文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我显然只是遗漏了一些东西,并且知道问题在于它正在创建一个空白输出,这就是它无法比较的原因.但是,如果有人可以对此有所了解,那就太好了 - 我没有将其隔离.
I'm obviously missing something simply, and know the problem is that it's creating a blank output which is why it can't compare. However if someone could shed some light on this it would be great - I haven't isolated it.
最终,我试图将存储在 txt 文件中的列表中的 md5sum
与存储在服务器上的列表进行比较.如果有错误,我需要它来报告.这是输出:
Ultimately, I'm trying to compare the md5sum
from a list stored in a txt file, to that stored on the server. If errors, I need it to report that. Here's the output:
root@vps [~/testinggrounds]# cat md5.txt | while read a b; do
> md5sum "$b" | read c d
> if [ "$a" != "$c" ] ; then
> echo "md5 of file $b does not match"
> fi
> done
md5 of file file1 does not match
md5 of file file2 does not match
root@vps [~/testinggrounds]# md5sum file*
2a53da1a6fbfc0bafdd96b0a2ea29515 file1
bcb35cddc47f3df844ff26e9e2167c96 file2
root@vps [~/testinggrounds]# cat md5.txt
2a53da1a6fbfc0bafdd96b0a2ea29515 file1
bcb35cddc47f3df844ff26e9e2167c96 file2
推荐答案
我不想争论.我只是尽量避免从内部循环中重复读取.
I'm not going to argue. I simply try to avoid double read from inside loops.
#! /bin/bash
cat md5.txt | while read sum file
do
prev_sum=$(md5sum $file | awk '{print $1}')
if [ "$sum" != "$prev_sum" ]
then
echo "md5 of file $file does not match"
else
echo "$file is fine"
fi
done
这篇关于Bash 循环比较文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文