Addresses #5902. As mentioned in https://github.com/bitcoin/bitcoin/commit/c65cc8cde30dd34a81962fda51a754f1cc0bdde8 the initial check caused issues with gitian builds, therefore this introduces a check for a DONT_CHECK_FOR_DOT_GIT environment variable to disable it if the variable is set to an arbitrary value. I'm not sure if this approach is suitable for gitian. Does gitian use something other then .git as GITDIR? Or what makes gitian builds special? Maybe @theuni can comment on that.
Also some minor lint fixups found by shellcheck.