linux - Bash循环比较文件

标签 linux bash loops if-statement while-loop

我显然只是遗漏了一些东西,并且知道问题在于它正在创建一个空白输出,这就是它无法比较的原因。但是,如果有人可以阐明这一点,那就太好了 - 我还没有将其孤立。

最后,我试图将存储在 txt 文件中的列表中的 md5sum 与存储在服务器上的列表进行比较。如果有错误,我需要它来报告。这是输出:

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

最佳答案

不是直接回答你的问题,而是md5sum(1) :

-c, --check
read MD5 sums from the FILEs and check them

喜欢:

$ ls
1.txt  2.txt  md5.txt
$ cat md5.txt
d3b07384d113edec49eaa6238ad5ff00  1.txt
c157a79031e1c40f85931829bc5fc552  2.txt
$ md5sum -c md5.txt
1.txt: OK
2.txt: OK

关于linux - Bash循环比较文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12413097/

相关文章:

linux - 将终端输出重定向到文件

linux - 检查linux脚本中文件是否存在

bash - 在 Mac 上通过命令行或快捷方式切换 wifi 连接

linux - pg_restore 分隔符

JavaScript - 循环是否比逐行离散编写更快?

c# - 如何在 C# 中比较相邻的双端队列元素?

c++ - 由于比较器,Visual C++ 抛出映射异常

c++ - 如何在 Linux 上延迟加载共享库

linux - 按列 Linux 在文件中排序

c++ - While/For 循环不会循环。 (C++)