I'm writing a script to ssh in to a list of machines and compare a variable to another value.. I've run into a problem (I have a couple workarounds, but at this point I'm just wondering why this method isn't working).
VAR=`ssh $i "awk -F: '/^bar/ {print \$2}' /local/foo.txt"`
($i would be a hostname. The hosts are trusted, no password prompt is given)
Example of foo.txt:
foo:123456:abcdef
bar:789012:ghijkl
baz:345678:mnopqr
I'm assuming it's a problem with quotes, or \'s needed somewhere. I've tried several methods (different quoting, using $() instead of ``, etc) but can't seem to get it right. My script is working correctly using the following:
VAR=`ssh $i "grep bar /local/foo.txt" | awk -F: '{print \$2}'`
Like I said, just a curiousity, any response is appreciated.
Forgot to post what my output was: awk is spitting out the whole matched line, not the 2nd section. Playing with the quotes and \'s a bit I seemed to get an error about "{print " command not found etc, as if there was a new line in there somewhere.