0

I have a template file with content

count : 2

ssh ubuntu@ip << EOF
 cat template|grep count|awk -F":" '{print $2}' > log
EOF

Desired output is 2

but it's printing count : 2

  • 1) You don't need cat. 2) Add the contents of the file to your question. – Nasir Riley Mar 26 at 1:48
  • What have you done to debug? – G-Man Mar 26 at 2:22
  • cat template| awk '/count/ {print $2}' | ssh ubuntu@ip "cat - > log" – GypsyCosmonaut Mar 26 at 3:19
1

The here-document << EOF creates a quoting context similar to "..." in which variables are expanded. You have a $2 in there, which is expanded by the local shell. (The apostrophes don't quote anything because they are inside the larger quoted string (heredoc) so they are not special characters. You can't "increase the quoting level" by nesting '...' inside "...")

Your local shell probably doesn't have a $2 parameter, so it expands to the empty string, and what the remote shell receives on stdin is:

cat template|grep count|awk -F":" '{print }' > log

And awk prints the whole line.

You can ask the heredoc operator not to expand variables by adding apostrophe-style quoting to terminating string, like this:

ssh ... << 'EOF'
...
EOF

If you do that to your example, the $2 will survive heredoc processing and be passed through ssh to the remote shell.

Additional tip: cat | grep | awk is doubly wasteful, since awkcan do all that. Your remote command is equivalent to

awk -F":" '/count/ {print $2}' template > log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.