Well, the answer is quite easy, and always the same:
nrpe/nagios doesn't work with the same environnement variables as you do when running in command line, especially the "current working dir".
when you write to "foo" (at the end of your ssh), ask yourself where this file will be located (the current working dir of nagios is /, if i'm correct). So specify the full path of "foo" (and do the same when you grep it).
the same applies to "newfoo", of course
Also, you correctly define the grep command if "GREPCMD=/usr/bin/grep" => use it
do the same for ssh => define the full path (/us/bin/ssh) and use it
well, I think you got the idea now, so if there are other mistakes, you will easily find them.
Also, when you have this kind of errors, it always pay to remove the "rm -f foo" and "rm -f newfoo" and have a look at the content of theses files when executed by nagios; you may have some surprises (usually bad surprises, of course :)).
hope this solves your problem and future problems as well