Replace the here-doc-execs with temporary files, for the exit status check does not work properly in such shells as dash - the exit status inside the block will always be an exit status of the command preceding the block immediately. No joy.