I have a shell script that reads two files which is basically an "ls" output in the following format
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
Using basic awk statements, I am trying to compare the filepermission (column 3); size (column 7); and filename (column 11) as follows, but it is printing even lines that are similar
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"
Based on Janos, comment I have updated the script as follows
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}')
Server1.txtandServer2.txtare identical, so the script outputs nothing. I made modifications to trigger the summary lines and it worked correctly for all 3 cases: different permission or size or missing file. So what is the problem here?lsoutput, replace it with dummy data that interacts with your script in the same way.