D
D
ds_ev3rest2014-03-25 22:26:15
bash
ds_ev3rest, 2014-03-25 22:26:15

Bash: How to compare array values ​​with another?

Good day! In general, I need to compare certain strings output by the nm program . I will describe my question in as much detail as possible.
There are .o files in the folder. Naturally, nm displays various data on them - U-characters (undefined) and others (B, T, C, etc.). The task is to determine the dependence of one file on another precisely by these features (in which file the function is not defined for which feature). For example, I will indicate the following: Let's
DfEEAph.png
analyze the first line: it turns out that the function print_ackermann is not defined in the first file on the left (the attribute is indicated in brackets), because it is Undef, but in the second file (after ->) it is defined (T).
I hope the point is clear. How I want to implement the solution itself: first, I implement a search through all .o files, look for the attribute U and write the names of the functions in a .txt file (only one file can be created), i.e.:

for file in $(ls *.o)
do 
nm $file | grep -e '\<U\>' | awk '{print $2}'
done | sort | uniq > file.txt

After that, I write all the lines from the file to arrays:
index=0
while read line; do
array[$index]="$line"
index=$(($index+1))
done < file1.txt

I don’t understand much about arrays, I found the above code on the Internet. And here the question arises: you need to find the same rows from the first array with the second (there will also be rows according to the features T, B, etc.) and write it out.
How can you compare each element of the array with another? How can this be done if I have only 1 file? Nested loops or arrays?
I would be very grateful if you would show me the principle of action / solution / whatever in the general code. As you can see, I did not regret the time to paint, I hope for your help. Thank you for your attention.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-03-26
@ds_ev3rest

the question arises: you need to find the same rows from the first array with the second ... How can you compare each element of the array with another?

Do not do this. bash supports associative arrays, where the key can be an arbitrary string. It would be logical to have an array that maps the function names to the files in which they are defined. Then filling and searching through such an array will look something like this:
#!/bin/bash

declare -A func2object

for file in $(ls *.o)
do
        for name in $(nm $file | grep -ie '\<T\>' | awk '{print $3}' | sort | uniq) ; do
                func2object[$name]="$file"
        done
done

for file in $(ls *.o)
do
        for name in $(nm $file | grep -e '\<U\>' | awk '{print $2}' | sort | uniq) ; do
                [ -z ${func2object[$name]} ] || echo "$file -> ${func2object[$name]} ($name)"
        done | sort | uniq
done

UPD: In the original version, assignment to array elements took place in a nested shell, so changes were not visible in the outer one.

D
ds_ev3rest, 2014-03-26
@ds_ev3rest

Thank you very much. From now on, the script outputs the results, almost as it should:
I just added "-n" and "->" to echo in order to output on one line. And how else to make the undefined function itself indicated in brackets (as in the example)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question