Help with displaying difference between two arrays (Bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with displaying difference between two arrays (Bash)
# 1  
Old 12-18-2011
Help with displaying difference between two arrays (Bash)

The code I have is kind of long, so I'm just posting the part I'm struggling with. I've found many examples online of comparing two arrays in Bash and printing the difference between them. I've tried them all, even mixed and matched some examples with no luck. I know this can't be as hard as I'm making it. Any advice much appreciated.

Code:
#!/bin/bash

diff(){
  awk 'BEGIN{RS=ORS=" "}
       {NR==FNR?a[$0]++:a[$0]--}
       END{for(k in a)if(a[k])print k}' <(echo -n "${!1}") <(echo -n "${!2}")
}

result()
{
   # Declare two associative arrays
   declare -A temp1 temp2


        for element in "${md5one[@]}"
        do
                ((temp1[$element]++))
        done

        for element in "${md5two[@]}"
        do
                ((temp2[$element]++))
        done

        for element in "${!temp1[@]}"
        do
                if [[ ${temp1[$element]} > 1 && ${temp2[$element]} > 1 ]]
                then
                        unset "temp1[$element]" "temp2[$element]"
                fi
        done

        Array3=($(diff temp1[@] temp2[@]))
        #echo -e "${Array3[@]}"
 
}
 
display(){
   clear
   echo -e "It should display here right...?\n\n"
   echo -e '\a'
   echo -e "${Array3[@]}"
 
}

# 2  
Old 12-18-2011
For starters, you're never calling any of these functions, so they never get run...

It's difficult to see what you're trying to do with that diff function, particularly in what files are being redirected into it, since that seems to depend entirely on script arguments naming variables that name files which aren't even hinted at in your post..

What are the contents of the two arrays, and what output do you want?
# 3  
Old 12-18-2011
Here's more of the code. Hopefully that will help.

Code:
#!/bin/bash

repeat()
{
   for i in "${files[@]}"
   do

     md5two+=( "$(md5sum $i | awk '{ print $1 }' )" )

   done
 
}

diff(){
  awk 'BEGIN{RS=ORS=" "}
       {NR==FNR?a[$0]++:a[$0]--}
       END{for(k in a)if(a[k])print k}' <(echo -n "${!1}") <(echo -n "${!2}")
}

result()
{
   # Declare two associative arrays
   declare -A temp1 temp2


        for element in "${md5one[@]}"
        do
                ((temp1[$element]++))
        done

        for element in "${md5two[@]}"
        do
                ((temp2[$element]++))
        done

        for element in "${!temp1[@]}"
        do
                if [[ ${temp1[$element]} > 1 && ${temp2[$element]} > 1 ]]
                then
                        unset "temp1[$element]" "temp2[$element]"
                fi
        done

        Array3=($(diff temp1[@] temp2[@]))
        #echo -e "${Array3[@]}"
 
}
 
display(){
   clear
   echo -e "It should be here right?\n\n"
   diff
   echo -e '\a'
   echo -e "${Array3[@]}"

}
 
# Make sure user is logged in as root
if [[ $EUID -ne 0 ]]; then

   echo "This script must be run as root" 1>&2
   exit 1

fi

# Get names of all modules
files=`find / -name "*.ko"` >> /tmp/mod0

# Read names of modules from temp file into array
while read line ;
do
        FILEZ[$files]="$line"
        files=$(($files+1))

done < /tmp/mod0

rm /tmp/mod0

# Get md5 hashes of all modules on the system and put them in an array
for i in "${files[@]}"
do

     md5one+=( "$(md5sum $i | awk '{ print $1 }' )" )

done

# Forever loop to keep monitoring constant
for ((  ;  ; ))
do
        repeat

        if [[ ${md5one[@]} == ${md5two[@]} ]]; then

                md5two=( )
        else
                monitor
                md5one=( )
                md5one=("${md5two[@]}")
                md5two=( )
        fi

done

# 4  
Old 12-19-2011
What would help a lot more would be the contents of the two arrays, and the output that you want.
# 5  
Old 12-19-2011
I outputted the contents of the three arrays to a text file but it was too much to fit here. You can view them here. (It's probably much easier to view the html source code) The first two are md5 hashes. The last array are file names.

The goal for my output is to show the file name of the md5 that has changed. I've been opening an .ko file in a hex editor and just changing something to test this. Once I get to where I can find the difference of the two md5 arrays I need to find the index of the difference to call that index on the array of file names to display. (Unless you know a better way) I just thought I'd focus on one problem at a time, but that is my overall goal.
# 6  
Old 12-19-2011
Rather that calculating md5sum sums on your files, why not use diff -q this is much more efficient as it can stop reading in the files as soon as it finds a byte different (or if the file sizes are not equal it can report that they vary without reading a thing).
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 12-20-2011
Sounds nice, but if more than one file is different it would end at the first one and not find the others, correct? I will remember the -q parameter for later. But I don't think it's what I can use here. Thanks though.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

2. Shell Programming and Scripting

bc,getopt and arrays in bash

trying to sum elements in an array using bc and getopt,i have a file with names and thier vaules if the names appears 3 times i should multiply its value with 3 then find the sum of all the elements together cat foo.txt max 2.3 henry 3 fransis 4.5 max 2.3 henry 3 max 2.3 it should... (1 Reply)
Discussion started by: elginmulizwa
1 Replies

3. Shell Programming and Scripting

Displaying difference of 2 files

Hi All, Got this 2 file namely a.txt and b.txt, i want to know how to extract the difference between the two files, the output will be written to a file. e.g. >>a.txt<< Nokia 1100 Nokia 1200 Nokia 1300 Nokia 1400 Nokia 1500 Nokia 1600 Nokia 1700 Nokia 1701 Nokia 1702 Sagem 1100... (3 Replies)
Discussion started by: shtobias
3 Replies

4. Shell Programming and Scripting

Difference between 2 arrays in perl

Hi, I have 2 arrays: @a=qw(19190289 18381856 12780546 10626296 9337410 8850557 7740161 8101063); @b=qw(18309897 17612870 10626296 16871843 7740161 19947571 18062861); $len=@a; print "<br> length of array1: $len<br>"; $len1=@b; print "<br> length of array2: $len1<br>"; The output... (3 Replies)
Discussion started by: vanitham
3 Replies

5. Shell Programming and Scripting

PHP Compare 2 Arrays find difference & case insensitive

Hi, I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1. I don't want to see any instances of array1 wich is not in array2 (here "the") Array1: This, is, the, data, of, array1 Array2: this, is, data, Of,... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. Shell Programming and Scripting

How to find difference between two arrays in Perl?

Hi, Could someone please help me with this? I have two arrays and I need to generate third array containing difference between the two. For example - @Array1 = ; @Array2 = ; I would like to find difference between these two and generate third array that contains the difference. In... (5 Replies)
Discussion started by: sncoupons
5 Replies

7. Shell Programming and Scripting

Compute difference between 2 arrays perl

Hi, I have 2 arrays. I have to compute symmetric difference! @arr=qw(19205134 18630215 18453487 18416242 18338715 18227590 17698645); @arr1=qw(18227590 18053561 17698645 16966777); #The code which i used is this! @union = @isect = @diff = (); %union = %isect = (); %count =... (3 Replies)
Discussion started by: vanitham
3 Replies

8. Shell Programming and Scripting

arrays and while loops in bash

hi guys, i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array.. i get a =: command not found error.. i=0 while do ${master_array}=${ARRAY} ((i++)) done is there something i am missing? (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. Shell Programming and Scripting

Arrays in bash.need help

:confused: Is it possible to delete array elements dynamically.For instance,consider an array( a b c d ) ,now can i delete array (the third element 'c').So that the array becomes array(a b d).. Thanks in advance!! (1 Reply)
Discussion started by: tj23
1 Replies
Login or Register to Ask a Question