Search code examples
whitespacequotesdouble-quotesplink

using PLINK to Send a remote command with single and double quotes


I'm trying to use plink on winXP to connect to a linux server and execute a command. Here's the command line I'm running:

plink some_profile cd "$(echo 'T:\somedir\somesubdir with space in it\' | sed 's_\\_/_g' | sed 's_T:_/media/drive1_g')";unrar x 'somefile.rar'

If I execute the command portion of this (start with the cd) on the linux box directly it works perfectly. But when I run it through plink, it fails with the following error:

bash: line 0: cd: /media/drive1/somedir/somesubdir: No such file or directory

I think I'm not quoting the command correctly when going through plink so it's not transferring the quotes needed to handle the spaces in the directories. Any ideas on how this should be quoted so that it works?

In case you're wondering why I'm even doing this, it's basically a script I'm running on Directory Opus where I can unrar the file I currently have highlighted (a samba mount). The directory and filename are passed to the script which will the unrar that file on my remote box.

Thanks!

EDIT: Problem solved thanks to the response from Mikel. Here is the line that works now in case anyone else comes across this later...

plink some_profile cd \"$(echo 'T:\somedir\somesubdir with space in it\' | sed 's_\\_/_g' | sed 's_T:_/media/drive1_g')\";unrar x 'somefile.rar'


Solution

  • You need to add another level of quotes, e.g.

    plink some_profile cd "\"$(echo 'T:\somedir\somesubdir with space in it\' | sed 's_\\_/_g' | sed 's_T:_/media/drive1_g')\"";unrar x 'somefile.rar'
    

    This is because you need one level of quotes on the Windows side, and one level of quotes on the Linux side.