Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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...

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
You are correct about the single quotes. I have updated the question to reflect this. –  Michael Irey 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 Irey Jun 28 '13 at 19:28
cat local-script.sh | ssh user@server bash

or whatever shell you want to use instead of bash

share|improve this answer
    
Excellent! And simple. –  Michael Irey Jun 28 '13 at 16:08

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!

share|improve this answer
    
That's not doing what the OP wants. –  Dennis Williamson Jun 28 '13 at 16:57
    
Corrected......... –  mdpc Jun 28 '13 at 17:01
    
What is the difference between <script.sh and $(<script.sh) ? –  Michael Irey Jun 28 '13 at 19:29
    
See the message from Dennis Williamson. I still go for simplicity, why involve more work for a program to do when a simpler construct can do the same and is far readable too. –  mdpc Jun 28 '13 at 19:36
    
@MichaelIrey: The difference is that a message stating that a pseudo-terminal is not allocated because of the redirection. You can suppress the resulting message by adding the -T option to your ssh command. –  Dennis Williamson Jun 28 '13 at 20:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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