#!/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.
#!/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
#!/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...
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
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.