Couple things. Well, this guy's probably long gone, but...
I don't have problems with the for loop as it is from the book:
for directory in $(find /opt/*/lib/pkgconfig -type d 2>/dev/null); do
pathappend $directory PKG_CONFIG_PATH
done
There's a separate check for /usr/local/lib/pkgconfig.
Second, the OP's suggestions for revised for loops would be terribly slow.
for directory in $(find / -name pkgconfig -type d 2>/dev/null); do
pathappend $directory PKG_CONFIG_PATH
done
That loop is going to scan everything in the root directory, first checking if it's named pkgconfig and then testing to see if it's a directory. If the type -d came first, that would help, but it would still have to check the name of every directory in /.
I'm closing this bug. If someone else has had errors with the extrapaths.sh script as used in the book please re-open it.