用于比较的Shell脚本

时间:2013-12-27 20:22:07

标签: shell awk

我有一个shell脚本,它读取两个文件,这些文件基本上是以下格式的“ls”输出

File 1 (Server1.txt):
386030    8 -rw-r--r--   1 bfdeploy wasgroup     4809 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/createFamilyAssessment.htm
386044    8 -rw-r--r--   1 bfdeploy wasgroup     6041 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/disabilityBenefits.htm
386179    8 -rw-r--r--   1 bfdeploy wasgroup     6780 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/staffSummaryAlerts.htm

File 2 (Server2.txt):
386030    8 -rw-r--r--   1 bfdeploy wasgroup     4809 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/createFamilyAssessment.htm
386044    8 -rw-r--r--   1 bfdeploy wasgroup     6041 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/disabilityBenefits.htm
386179    8 -rw-r--r--   1 bfdeploy wasgroup     6780 Jul 15 15:50 /apps/ibm/httpserver/htdocs/wp/en_robohelp/staffSummaryAlerts.htm

使用基本的awk语句,我试图比较filepermission(第3列);大小(第7栏);和文件名(第11列)如下,但它打印的是相似的行

while read line
    do
            filename1=$(echo "$line"|awk '{print $11}')
            filesize1=$(echo "$line"|awk '{print $7}')
            filepermission1=$(echo "$line"|awk '{print $3}')
            lineinserver2=$(grep "$filename1" "$SERVER2.txt")
            if [ $? -eq 1 ]
            then
                    echo "$filename1 is in $SERVER1 $COMPDIR but not present in $SERVER2 $COMPDIR" >> $DIFFSUMMARYFILE
            else
                    filesize2=$(echo "$lineinserver2"|awk '{print $7}')
            #       echo $lineinserver2
            #       echo $filesize2
                    filepermission2=$(echo "$lineinserver2"|awk '{print $3}')
            #       echo $filepermission2
                    if [ $filesize1 != $filesize2 ]
                    then
                            echo "$filename1 on $SERVER1 has a size of $filesize1 and on $SERVER2 has a size of $filesize2" >> $DIFFSUMMARYFILE
                    fi
                    if [ "$filepermission1" != "$filepermission2" ]
                    then
                            echo "$filename1 on $SERVER1 has permission of $filepermission1 and on $SERVER2 has permission of $filepermission2" >> $DIFFSUMMARYFILE
                    fi

            fi
    done < "$SERVER1.txt"

基于Janos,评论我更新了脚本如下

 while read filepermission1 fileseize1 filename1; do
            read filepermission2 filesize2 filename2 < <(grep "$filename1" "$SERVER2.txt" | awk '{print $3, $7, $11}')
            if [ $? -eq 1 ]; then
                    echo "$filename1 is in $SERVER1 $COMPDIR but not present in $SERVER2 $COMPDIR" >> $DIFFSUMMARYFILE
            else
                    if [ $filesize1 != $filesize2 ]; then
                    echo "$filename1 on $SERVER1 has a size of $filesize1 and $filename2 on $SERVER2 has a size of $filesize2" >> $DIFFSUMMARYFILE
                    fi
                    if [ "$filepermission1" != "$filepermission2" ]; then
                    echo "$filename1 on $SERVER1 has a permission of $filepermission1 and $filename2 on $SERVER2 has a permission of $filepermission2" >> $DIFFSUMMARYFILE
                    fi
            fi
    done < <(grep -xvf $SERVER2.txt $SERVER1.txt|awk '{print $3, $7, $11}')

1 个答案:

答案 0 :(得分:2)

您的脚本似乎对我有用:

  • 如果示例文件相同,则无法正确输出
  • 如果文件大小不同,则会正确报告
  • 如果权限不同,则会正确报告
  • 如果第二个文件中缺少某个文件,则会正确报告

为了更清楚地看到,我使用现代bash结构更有效地重写了它:

#!/bin/bash
SERVER1=server1
SERVER2=server2
while IFS=' ' read filepermission1 filesize1 filename1; do
    IFS=' ' read filepermission2 filesize2 filename2 < <(grep "$filename1" "$SERVER2.txt" | awk '{print $3, $7, $11}')
    if [ $? -eq 1 ]; then
        echo "$filename1 is in $SERVER1 $COMPDIR but not present in $SERVER2 $COMPDIR"
    else
        if [ $filesize1 != $filesize2 ]; then
            echo "$filename1 on $SERVER1 has a size of $filesize1 and on $SERVER2 has a size of $filesize2"
        fi
        if [ "$filepermission1" != "$filepermission2" ]; then
            echo "$filename1 on $SERVER1 has permission of $filepermission1 and on $SERVER2 has permission of $filepermission2"
        fi
    fi
done < <(grep -vxf $SERVER2.txt $SERVER1.txt | awk '{print $3, $7, $11}')

<强>更新

根据您的评论,似乎列不在您期望的位置。要看清楚,只使用echo行运行,如下所示:

#!/bin/bash
SERVER1=server1
SERVER2=server2
while read filepermission1 filesize1 filename1; do
    read filepermission2 filesize2 filename2 < <(grep "$filename1" "$SERVER2.txt" | awk '{print $3, $7, $11}')
    echo filepermission1=$filepermission1
    echo filesize1=$filesize1
    echo filename1=$filename1
    echo filepermission2=$filepermission2
    echo filesize2=$filesize2
    echo filename2=$filename2
done < <(grep -vxf $SERVER2.txt $SERVER1.txt | awk '{print $3, $7, $11}')

我希望根据输出你应该看得更清楚。

<强>结论

似乎问题是由于原始脚本或调用脚本中的IFS=...操作造成的。作为一种解决方法,我在IFS=' '命令前插入read,这很重要。理想情况下,调用脚本应更改为使用IFS='...' cmd,以便IFS操作仅对cmd有效。然后可以安全地删除上述脚本中的IFS=' ',因为脚本应该可以正常使用默认值。