Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Imagine a shell script on the remote server as

#!/bin/bash
rm /test.x

How can I (if possible) to execute this script from my local machine to delete /test.x file on my local machine. Obviously, the solution should be with something like ssh authorization, but without downloading the script file from the remote server.

In fact, I want to use the remote script as a provider of shell commands to be run on the local machine.

share|improve this question
    
put it on a filesystem shared via NFS. Mount the NFS filesystem on the local machine, and run /path/to/nfs/mount/script.sh from the local machine. The local machine's /test.x file will be removed. –  Tim Kennedy Jan 19 '13 at 20:57
    
but without downloading the script file from the remote server: How do you expect the remote script to work on your local machine without sending the script content to the local machine ?!? –  BatchyX Jan 19 '13 at 21:00
    
@BatchyX I mean reading the commands somehow, instead of downloading the entire script and executing it locally. –  All Jan 19 '13 at 21:03
    
@All: bash stores command definitions by more-or-less saving their definition (use set to see that). You won't gain anything by trying to send the bare minimum to make the command work. Just think about the dependency hell if command do_that depends on do_this to work properly. You could still do it proxy-style (e.g. do_that () { download_do_that_definition_from_server && do_that; }), but it's still much more complicated than downloading the entire script and feeding it directly into the interpreter. –  BatchyX Jan 19 '13 at 21:14
1  
What are you trying to do? In many cases the idea you come up with isn't possible, or there are much better ways of getting to your objective. –  vonbrand Jan 21 '13 at 17:44
add comment

2 Answers

You'll need to download the content of the script in some way. You could do

ssh remote-host cat script.bash | bash

But that would have the same kind of problem as:

cat script.bash | bash

namely that stdin within the script would be the script itself (which could be an issue if commands within the script need to get some input from the user).

Then, a better alternative (but you'd need a shell with support for process substitution like ksh, zsh or bash) would be:

bash <(ssh remote-host cat script.bash)

Both approaches do download the script in that they retrieve its content, but they don't store it locally. Instead the content is fed to a pipe whose other end is read and interpreted by bash.

You can also have the content of the remote script executed in the current bash process with:

eval "$(ssh remote-host cat script.bash)"

But that downloads the script fully (and stores it in memory) before running it.

The obvious solution would be to do:

. <(ssh remote-host cat script.bash)

But beware that some versions of bash have issues with that.

share|improve this answer
    
Definitely, we need to somehow fetch the script content (commands), and using bash is quite, as it' my default. Seems a subtle and practical solution. –  All Jan 19 '13 at 21:06
add comment

Mount the remote filesystem containing the script with sshfs. This makes the script a local file which you know how to execute.

mkdir /path/to/remote-host
sshfs remote-host:/ /path/to/remote-host
/path/to/remote-host/path/to/script
share|improve this answer
    
+1 Wow, perfect and simple! –  Christopher Jan 21 '13 at 1:08
add comment

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.