Question

My script is executing the following line:

ssh $REMOTE_USER@${SUPPORTED_SERVERS[$i]} "gtar -zcvf $TAR_FILE `find $LOCAL_PATH -name *$DATE*`

Now, the problem is that find command is being executed on the local machine and I need it to be executed on the remote one.

Please help, Thanks

Était-ce utile?

La solution

Use $() rather than backtick ` and additionaly escape it with backslash to avoid executing the command on the local machine:

ssh $REMOTE_USER@${SUPPORTED_SERVERS[$i]} "gtar -zcvf $TAR_FILE \$(find $LOCAL_PATH -name *$DATE*)"

Autres conseils

Make a script, copy it to the server and run that. It may seem pointless now for such a simple thing, but you'll probably thank me later.

When executing a command over SSH, you need to escape any special characters that should not be evaluated locally. Escape the backticks to have them evaluated on the remote server:

ssh $REMOTE_USER@${SUPPORTED_SERVERS[$i]} "gtar -zcvf $TAR_FILE \`find $LOCAL_PATH -name *$DATE*\`

Assuming $TAR_FILE, $LOCAL_PATH and $DATE are local variables, otherwise escape them also. (They would need to exist as environment variables on the remote server)

Alternative

Like @RobinGreen points out: It is often better to make a script on the remote server and execute that over SSH.

#!/bin/sh
# This is the remote script
# Use positional arguments $1 - $3 and make a tarball
gtar -zcvf $1 $(find $2) -name "*$3*"

Call it like this

ssh $REMOTE_USER@${SUPPORTED_SERVERS[$i]} "/path/to/remote/script $TAR_FILE $LOCAL_PATH $DATE"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top