Answer the question
In order to leave comments, you need to log in
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
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
index=0
while read line; do
array[$index]="$line"
index=$(($index+1))
done < file1.txt
Answer the question
In order to leave comments, you need to log in
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?
#!/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
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 questionAsk a Question
731 491 924 answers to any question