2

所以我正在制作的脚本的目的是比较两个文件,同时读取其中包含文件路径名的列表......

while read compareFile <&3; do     
 if [[ ! $server =~ [^[:space:]] ]] ; then  #empty line exception
  continue
 fi   
    echo "Comparing file - $compareFile"
 if diff "$compareFile" _(other file from loop?_) >/dev/null ; then
    echo Same
 else
     echo Different
 fi   
 done 3</infanass/dev/admin/filestoCompare.txt

我需要能够通过两个 while read 循环同时比较来自两个不同列表的文件......这甚至可能吗?

4

4 回答 4

4

我想我会按照这些思路进行重组:

while true
do
   read -u3 line1 || break
   read -u4 line2 || break

   # do whatever...
done 3< file1 4< file2

它使用单个循环,并且当任一输入文件到达文件末尾时将退出该循环。如果您想完全读取这两个文件,即使一个提前结束,逻辑也会稍微复杂一些......

于 2013-07-10T19:59:18.380 回答
1

不是很明白你想要实现什么,但下一个:

while read -r file1 file2
do
    echo diff "$file1" "$file2"
done < <(paste <(grep . list1.txt) <(grep . list2.txt))

其中list1.txt包含:

file1.txt
file2.txt

file3.txt

file4.txt
file5.txt

并且list2.txt包含:

another1.txt

another2.txt
another3.txt
another4.txt

another5.txt

产生下一个输出:

diff file1.txt another1.txt
diff file2.txt another2.txt
diff file3.txt another3.txt
diff file4.txt another4.txt
diff file5.txt another5.txt

如果您满意,请删除echo前面的。diff

于 2013-07-10T19:57:58.223 回答
1

如果我理解正确的话……是的。这是一个在锁步中循环遍历两个文件的示例

exec 3<filelist1.txt
exec 4<filelist2.txt
while read -r file1 <&3 && read -r file2 <&4; do echo ${file1}","${file2}; done
exec 3>&- 4>&-
于 2013-07-10T19:58:24.677 回答
0
while read newfile <&3; do   
 if [[ ! $newfile =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
 #
 while read oldfile <&3; do   
 if [[ ! $oldfile =~ [^[:space:]] ]] ; then  #empty line exception
    continue
 fi   
    echo Comparing "$newfile" with "$oldfile"
    #
    if diff "$newfile" "$oldfile" >/dev/null ; then
      echo The files compared are the same. No changes were made.
    else
        echo The files compared are different.
        #
    fi    
  done 3</home/u0146121/test/oldfiles.txt
 done 3</home/u0146121/test/newfiles.txt
于 2013-07-11T12:58:52.247 回答