Hello everyone,
I need help comparing 2 arrays. the first array is static; the second array is not ..
array1=( "macOS Mojave" "iTunes" )
cd /Volumes
array2=( * )
# output of array2
macOS Mojave
iTunes
Mac me
The problem occurs when I compare the arrays with the following code -
DIFF=`echo "${array1[@]}" "${array2[@]}" | tr ' ' '\n' | sort | uniq -u`
for x in "${DIFF[@]}"
do
echo -e "$x"
done
# wrong result
Mac
me
# desired result
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?
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.
length=${#DIFF[@]}
for (( i=0; i<${length}; i++ ));
do
echo ${DIFF}
done
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?
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