Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Compare bash arrays issue


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Compare bash arrays issue

Hello everyone,
I need help comparing 2 arrays. the first array is static; the second array is not ..

Code:
array1=( "macOS Mojave" "iTunes" )
cd /Volumes
array2=( * )

# output of array2
Code:
macOS Mojave
iTunes
Mac me

The problem occurs when I compare the arrays with the following code -

Code:
 DIFF=`echo "${array1[@]}" "${array2[@]}" | tr ' ' '\n' | sort | uniq -u`

for x in "${DIFF[@]}"
        do
                echo -e "$x"
        done

# wrong result
Code:
Mac
me

# desired result
Code:
Mac me

I'm sending the output via email and it still splits up "Mac" "me" on separate lines. How could I keep the DIFF array intact? Not separating the words when I do the compare?

Last edited by Scrutinizer; 3 Days Ago at 12:16 AM.. Reason: code tags
# 2  
Quote:
Originally Posted by trexthurman
[..]How could I keep the DIFF array intact? Not separating the words when I do the compare?
Indeed, the operation should be line based, rather than word based, since file names can have spaces in them.

Compare:
Code:
echo "${array1[@]}" "${array2[@]}" | tr ' ' '\n' | sort | uniq -u

with:
Code:
printf "%s\n" "${array1[@]}" "${array2[@]}" | sort | uniq -u



--
Note: File names could even have newlines in them, but it is rare so let's leave that for now......

Last edited by Scrutinizer; 2 Days Ago at 04:12 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
$DIFF in not an array but a scalar variable.
Try also
Code:
IFS=$'\n'
echo "${array1[*]}"$'\n'"${array2[*]}" | sort | uniq -u
Mac me

Don't forget to reset IFS to your default value.
This User Gave Thanks to RudiC For This Post:
# 4  
Your suggestions work for outputting to standard output but as I was testing the email portion, if there are 2 entries as differences - it treated them as 1 element in the array as oppose to 2 elements. I was hoping to have both differences in the array as its own element.

Code:
length=${#DIFF[@]}
for (( i=0; i<${length}; i++ ));
do
echo ${DIFF[i]}
done

# wrong result
Code:
DIFF[0] Mac me 
Windows
DIFF[0] Mac me Windows

# Desired result
Code:
DIFF[0] Mac me
DIFF[1] Windows


Last edited by Scrutinizer; 2 Days Ago at 02:40 PM.. Reason: code tags
# 5  
Again: DIFF is a scalar NOT an array the way you create / populate it. There is NO ${DIFF[1]}!


Wouldn't it be better to show the big picture of your problem, so a tailored solution can be found? What are you after, in the end?
# 6  
Here is the code I'm working on. After comparing both arrays, if multiple differences are detected between arr1 and arr2. I should be able to send an email for each difference found. Below, looks like I'm still treating the scalar as an array? Is there a way to make the scalar into array after comparing so I can access the elements individually?

Code:
arr1=( "macOS Mojave" "iTunes" )
cd /Volumes
arr2=( * )

IFS=$'\n'
DIFF=`echo "${arr1[*]}"$'\n'"${arr2[*]}" | sort | uniq -u`
VOLCOUNT=`printf "%s\n" "${arr1[@]}" "${arr2[@]}" | sort | uniq -u | wc -l`
if [ "$VOLCOUNT" -gt "$COUNT" ]; then
        for x in "${DIFF[@]}"
        do
                echo -e "To: ${TO}\nSubject: Alert: ${SUBJECT}$x \n\n$SIGN" | $MAIL -a gmail ${TO}
        done
else
        >/dev/null 2>&1
fi
unset $IFS
exit 0


Last edited by Scrutinizer; 2 Days Ago at 02:41 PM.. Reason: code tags
# 7  
Quote:
Originally Posted by trexthurman
...
Is there a way to make the scalar into array after comparing so I can access the elements individually?
Yes. You did it twice with arr1 and arr2. It can vary from shell to shell, so it's best to always post your OS and shell.



Quote:
unset $IFS
Don't unset IFS - reset it to its former (saved!?) value.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two arrays by values [BASH]

I have 2 arrays of values for example A1 ={10 15 3 21} A2 ={10 15 3 22} I need to check which one has greater values. However: A1 ={10 15 3 21} A2 ={10 15 3 21 3} - this one would be greater. A1 ={10 15 5 21} - this one greater A2 ={10 15 3 21} Basically, I want to compare patch... (6 Replies)
Discussion started by: Jutsimitsu
6 Replies

2. Shell Programming and Scripting

Compare two arrays

Hi, I am trying to compare two lists that are held in two variables so I believe I need to access the array elements to compare these. I am using ksh 88 and the code I have tried is below: for file in ${origfilelist} do if ]] then print -- "File ${file}... (3 Replies)
Discussion started by: frodo61
3 Replies

3. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

4. 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

5. Shell Programming and Scripting

perl: compare two arrays

Hi friends, I want to compare two arrays and find matched one using perl? Also, I want to delete unmatched one. Plz suggest me solution (1 Reply)
Discussion started by: Renesh
1 Replies

6. Shell Programming and Scripting

Perl Compare 2 Arrays

Hello, Consider the following 2 arrays: Array1 = qw(Fa0/0 Fa0/1 Fa0/2 Fa0/3); Array1 = qw(Fa0/1 Fa0/2 Fa0/3 Fa0/4); I want to compare the following 2 arrays as follows: Take specific action when elements of Array1 that doesn't exist in Array2 (in my example: Fa0/0). Take another... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

7. Shell Programming and Scripting

Using Bash scripting to compare arrays looking for matches

I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences. Thanks in advance! Karl (9 Replies)
Discussion started by: karlp
9 Replies

8. Shell Programming and Scripting

compare 2 arrays in perl

Hi Im supposed to compare lines in a file : KB0005 1019 T IFVATVPVI 0.691 PKC YES KB0005 1036 T YFLQTSQQL 0.785 PKC YES KB0005 1037 S FLQTSQQLK 0.585 DNAPK YES KB0005 1045 S KQLESEGRS 0.669 PKC YES KB0005 1045 S KQLESEGRS 0.880 unsp YES KB204320 1019 T IFVATVPVI 0.699 PKC YES ... (7 Replies)
Discussion started by: karla
7 Replies

9. Shell Programming and Scripting

compare/match arrays

Hi there all, I am having a question. Is it posible to compare elements of 2 different arrays? For example I got Array 1 | Array 2 123_abc | 123_bcd 123_bcd | 234_bcd 234_abc | 567_abc 234_bcd | 123_abc than the match is 123_abc & 234_bcd and non of the others. So... (3 Replies)
Discussion started by: draco
3 Replies

10. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

Featured Tech Videos