【问题标题】:Bash loop to compare files用于比较文件的 Bash 循环
【发布时间】:2012-09-06 22:40:03
【问题描述】:

我显然只是简单地遗漏了一些东西,并且知道问题在于它正在创建一个空白输出,这就是它无法比较的原因。但是,如果有人能对此有所了解,那就太好了 - 我没有孤立它。

最终,我试图将存储在 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

【问题讨论】:

    标签: linux bash loops if-statement while-loop


    【解决方案1】:

    您遇到的问题是您的内部读取是在子外壳中执行的。在 bash 中,当您通过管道传输命令时会创建一个子 shell。一旦 subshel​​l 退出,变量 $c 和 $d 就消失了。您可以使用进程替换来避免子shell:

    while read -r -u3 sum filename; do
       read -r cursum _ < <(md5sum "$filename")
       if [[ $sum != $cursum ]]; then
          printf 'md5 of file %s does not match\n' "$filename"
       fi
    done 3<md5.txt
    

    重定向3&lt;md5.txt 导致文件作为文件描述符3 打开。read-u 3 选项导致它从该文件描述符中读取。内部的read 仍然从标准输入读取。

    【讨论】:

    • 内部 read 命令没有从封闭的 while 循环继承标准输入;它通过管道连接到md5sum。您正在通过在内部读取上使用进程替换(而不是管道)来解决问题,而不是通过对外部循环使用单独的文件描述符。
    【解决方案2】:

    不是直接回答你的问题,而是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
    

    【讨论】:

    • 这可能是最好的路线,但我打赌并非所有版本的 md5sum 都支持这一点。这个问题被标记为 linux。
    【解决方案3】:

    我不会争论。我只是尽量避免从内部循环中重复读取。

    #! /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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 2015-10-01
      • 2015-05-23
      相关资源
      最近更新 更多