0

I am trying to execute a script on remote servers, passing the script as the last argument

ntrs exec-all-ubuntu --exec `cat << 'EOF'

  echo "$(pwd)"
  echo "$foobar"

EOF`

The problem is that the values in the text are sent as separate arguments, echo is the first arg and the pwd value is a second separate arg, but I want just one argument as a string

The arguments end up looking like this:

[ '--exec', 'echo', '"$(pwd)"', 'echo', '"$foobar"' ]

but I am looking for something literal with newlines:

[ '--exec', '   echo "$(pwd)"\n\n echo "$foobar"\n ' ]

I also tried using this:

ntrs exec-all-ubuntu --exec `read -d << EOF
    select c1, c2 from foo
    where c1='something'
EOF`

but that string is empty

4

You can simply use a regular string with embedded newlines:

ntrs exec-all-ubuntu --exec '
  echo "$(pwd)"
  echo "$foobar"
'
  • thanks that's very good! appreciate it – Alexander Mills May 14 at 18:41
3

From the manual page for bash(1):

The format of here-documents is:

      [n]<<[-]word
              here-document
      delimiter

No parameter and variable expansion, command substitution, arithmetic
expansion, or pathname expansion is performed on word.  If any part of
word is quoted, the delimiter is the result of quote removal on word,
and the lines in the here-document are not expanded.

Given that your post is tagged I'd suggest:

ntrs exec-all-ubuntu --exec "$(cat << 'EOF'

  echo "$(pwd)"
  echo "$foobar"

EOF
)"

Lastly,

echo "$(pwd)"

might be better as simply:

pwd
0

Jim L. is right, but here's an even less verbose way?

ntrs exec-all-ubuntu --exec "`cat << 'EOF'

  echo "$(pwd)"

  echo "$foobar"

EOF
`"

the double quotes around the bacticks are the way to go?

  • 1
    The only difference is that this uses backticks instead of $(), no? – Benjamin W. May 14 at 18:16
  • yeah pretty much, I wish there was an easier way to do this, maybe by avoid cat altogether? can't we just use < EOF or something and drop cat? – Alexander Mills May 14 at 18:18
  • It look like --exec expects a string as its argument, can't you just use --exec "echo \"$PWD\"; echo \"$foobar\""? – Benjamin W. May 14 at 18:32

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.