4

So I have been running local scripts fine on a remote server:

ssh user@server "`cat local-script.sh`"

However, today I have a script that has both single and double quotes in it. Which causes the script to fail because the output of cat local-script.sh is wrapped in quotes. With out modifying the script itself, is there a better way to handle this?

I thought this may work:

ssh user@server $(<local-script.sh)

But is does not seem to do anything...

Michael
  • 801
  • 1
  • 7
  • 15

3 Answers3

8
cat local-script.sh | ssh user@server bash

or whatever shell you want to use instead of bash

johnshen64
  • 5,865
  • 24
  • 17
4

With the following test script:

#!/bin/bash
uname -n
a=(b c 'd e' f)
echo "${#a[@]}"
b=(1 "2 3" 4)
echo "${#b[@]}"

The output should be the remote host name followed by 4 and 3 when executed using:

ssh user@server "$(<scriptname)"

This shows that the quotes inside the script are being handled correctly. If the quotes weren't being handled correctly, the numbers output would be 5 and 4 due to word splitting. Note the quotes around the command substitution.

By the way, the first command you posted doesn't work. The single quotes prevent the backticks from being evaluated locally.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • You are correct about the single quotes. I have updated the question to reflect this. – Michael Jun 28 '13 at 17:12
  • I have awarded the correct answer to you, because it was closest to what I originally had. I was just missing the quotes. Also because you provided a testable script. – Michael Jun 28 '13 at 19:28
1

Assume that the script is in file script.sh. What the heck is the problem with running:

 ssh -T user@remote.host.com <script.sh

This works for me!

mdpc
  • 11,856
  • 28
  • 53
  • 67