1

When I run this at the command line it works correctly

awk 'BEGIN{FS=OFS=","}/^AAA/{$3="QQQ"}1' test.txt > newtest.txt

However when I try and run this in a bash script which SSH's in to a remote server it fails with a syntax error, but I can't see where it fails. It looks like it's pointing the the FS in the command.

I'm doing the following:

sshpass -p "password" ssh user@192.168.0.1 /bin/bash << EOF

  echo password | sudo -u root -S awk 'BEGIN{FS=OFS=","}/^AAA/{$3="QQQ"}1' test.txt  > newtest.txt

EOF

The SSH connection works fine, but the awk command fails. Any ideas how to resolve this ?

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Tom
  • 1,436
  • 24
  • 50
  • BTW, that's an interesting command -- you clearly don't need `sudo` to open `newtest.txt` (since the open is done by your shell before `sudo` is run), so all you're escalating privileges for is the read of `test.txt`. But if `test.txt` is so security-sensitive that only root can read it, why would you want to store something derived from it under a less-privileged account? – Charles Duffy Mar 18 '19 at 16:19

1 Answers1

3

<<EOF in an unquoted heredoc. Inside these, all parameter expansions are performed, regardless of any internal quoting.

If you want $3 to be passed to awk without substituting the value for that expansion in the outer shell, you need to use a quoted heredoc: <<'EOF', not <<EOF.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441