U
U
uskaritel2018-10-03 11:18:42
linux
uskaritel, 2018-10-03 11:18:42

How to write a simple Bash loop that will compare the contents of arrays?

Good afternoon,
there are three arrays. The first with the user ID, two with the same number of indexes but different values, but only 0 or 1, this is the user's status in different systems, is it active. It is necessary to write a cycle (function) that will compare the array indices one by one and do something.
For example, if the index 1451 in the first array is greater than in the second one, then write the user ID + the final value of the status comparison from the two arrays True.
I did something like this but it doesn't work.


for i in "{${#STATUS_AR[@]}}"; do
if [ "$STATUS_AR[$i]" -lt "$STATUS_AD_AR[$i]" ]; then
echo "True"
fi
done

Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Ratkin, 2018-10-03
@Hanharr

I wrote such a thing to remove elements from the first array that are in the second:

for USER in "${!USER_LIST[@]}"
do
    for EXCEPTION in "${EXCEPTION_LIST[@]}"
    do
        if 
        then
            unset USER_LIST[$USER]
        fi
    done
done

I suspect that in your case there will be a loop with two nested loops.

3
3vi1_0n3, 2018-10-26
@3vi1_0n3

ID_AR=( id1 id2 )
STATUS_AR=( 1 0 )
STATUS_AD_AR=( 1 1 )

for i in $(seq 1 ${#STATUS_AR}); do
        if [ ${STATUS_AR[$i]} -lt ${STATUS_AD_AR[$i]} ]; then
                echo ${ID_AR[$i]}: "True"
        fi
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question