So here is the final working code for me which is almost similar to what MadeInGermany said earlier.
Code:
opFile="FilePairs.txt"
rm -rf $opFile
find . -name "*txt" -path "*keyA1/*" -path "*keyA2/*" > tmpfileA.txt
find . -name "*txt" -path "*keyB1/*" -path "*keyB2/*" > tmpfileB.txt
while read -r fileA
do
while read -r fileB
do
if [[ "$fileA" < "$fileB" ]] ; then
printf '%s %s \n' "$fileA" "$fileB"
fi
done < tmpfileB.txt >> $opFile;
done < tmpfileA.txt
Difference compared to my old approach is that:
1) The find command output was saved separately
2) While loop read in Lines from there
3) Important - earlier I used to run bash script as a command and I used to redirect terminal output to the intended output file like this:
Code:
>> ./myScript.sh > FilePairs.txt
Now as suggested by MadeInGermany the redirection is directly happening from the end of the inner while loop.
NOTE - the blank spaces in the if statement condition [[ "$fileA" < "$fileB" ]] are important
Thanks everyone
