2017-10-10 47 views
0

我试图比较所有文件对之间的差异。但是,为了比较文件对,我需要能够访问for循环中的当前元素之后的元素。这是我目前拥有的代码 -BASH - 将文件名中的下一个元素用于循环

#!/bin/bash 
for file in $(find -type f); do 
    b = $(file+1) -- I know this is not the correct way to access the next element 
    diff $(file) $(b) >/dev/null 
    if [ #? -eq 0 ] 
     echo $(file) and $(b) are the same 
    else 
     echo $(file) and $(b) are not the same 
    fi 
done 

与访问下一个元素的方式任何援助将不胜感激。

回答

1

您可以访问上一个元素,而不是下一个元素:存储迭代的当前文件名并在下一个文件中重用。喜欢的东西:

#!/usr/env/bin bash 

declare previous="" 

for file in $(find -type f); do 
    if [[ -n $previous ]]; then 
    if diff --brief "${previous}" "${file}" > /dev/null; then 
     echo "${previous} and ${file} are the same" 
    else 
     echo "${previous} and ${file} are not the same" 
    fi 
    fi 
    previous="${file}" 
done 

注意:如果你真的想附上变量名,使用大括号(如上面的代码),而不是括号(在你的代码):${file}的计算结果为变量的值file$(file)不带参数调用命令file,并返回错误消息。

+0

太棒了,完美的作品:)非常感谢。另外关于花括号和圆括号,我意识到在提交后 - 我没有正确复制终端上的文本。再次感谢! – GregHamilton2

相关问题