How to get difference of two array?

Required Unix shell script which will identify the difference of String2 based on string1. I am facing this issue and unable to achieve the result.

String1= {1 3 lok kam}
String2= {3 kam 5}

Result should be like below:

Data is matching for: 3
Data is matching for: kam
Data is not matching for: 5

Thanks in advance.

Here is one way to do so

#!/bin/bash

String1="1 3 lok kam"
String2="3 kam 5"

for i in ${String2[@]}; do
    grep $i <<<$String1 >/dev/null
    [ $? -eq 0 ] && echo "Data is matching for:$i" || echo "Data is not matching for: $i"
done
$ bash test
Data is matching for:3
Data is matching for:kam
Data is not matching for: 5

grep accepts partial match, why not set indicator for no match, iterate string1 inside string 2 and check for =, if match print matching, set indicator and break, and after inner loop, if indicator says no match, print no match.

Yes. DGPickett I did a mistake

#!/bin/bash

String1="1 3 lok kam"
String2="3 kam 5"

for i in ${String2[@]}; do
   f=0
   for j in ${String1[@]}; do 
    	[ "$i" == "$j" ] && f=1 && break
   done
   [ "$f" -eq "1" ] && echo "Data is matching for: $i" || echo "Data is not matching for: $i"
done

With actual arrays (bash, ksh):

#!/bin/bash
array1=(1 3 lok kam)                                                              
array2=(3 kam 5)                                                              
for i in "${array2[@]}"
do
  for j in "${array1[@]}"
  do
    if [ "$i" = "$j" ]
    then
      printf "Data is matching for: %s\n" "$i"
      continue 2
    fi
  done
  printf "Data is not matching for: %s\n" "$i"
done

--
@Akshay: for i in $String1; do will do the same thing...

Hello,

For ksh it should work like the following.

array1="1 3 lok kam"                                                           
array2="3 kam 5"
                                                     
set -A array2_array2 $array2         
set -A array1_array1 $array1
 
for i in "${array2_array2[@]}"
do
  for j in "${array1_array1[@]}"
  do
    if [ "$i" = "$j" ]
    then
      printf "Data is matching for: %s\n" "$i"
      continue 2
    fi
  done
  printf "Data is not matching for: %s\n" "$i"
done

Output will be as follows.

Data is matching for: 3
Data is matching for: kam
Data is not matching for: 5

Thanks,
R. Singh

Thanks to everyone. now I am running my code successfully

Nice, I never noticed the continue N option, which saves an indicator variable. Using a function and return is another way to get to an outer loop in many languages, without the dreaded goto.