3

After (Powershell: Properly escape apostrophes) I still haven't figured out how to get those escapes right when executing a command over plink, coming from powershell.

What I am trying to execute at the linux host, is

df -k / | tail -1 | awk '{print $4}'

This just gives me the amount of free KB at /.

Since I am at my powershell (Windows), I have to build a plink command to get the command to the server. Naively, it looks like this:

& plink -ssh -l $user -batch -pw $pw $user@$IP df -k / | tail -1 | awk '{print $4}'

This fails, because PowerShell jumps in at the | and searches for a command tail. So, I have to escape the pipes like that:

& plink -ssh -l $user -batch -pw $pw $user@$IP df -k / `| tail -1 `| awk '{print $4}'

This gives me awk: cmd. line:1: Unexpected end of string.

I think, I have to escape the $4 twice, because PowerShell and the Linux Bash try to replace it with a variable. But how can I do that? Using

\`$4

doesn't change the message.

Bowi
  • 1,637
  • 2
  • 20
  • 41

1 Answers1

3

You can use plink -ssh -l $user -batch -pw $pw $user@$IP "df -k / | tail -1 | awk '{print `$4}'".

Powershell escaping in general:

Escaping characters between two double quotes:

  • $ needs to be escaped with `$
  • " needs to be escaped with `"
  • ` needs to be escaped with ``

Exception:
A trailing (at the end of the double quote) $ doesn't need to be escaped at all.

Escaping characters between two single quotes:

  • ' needs to be escaped with ''

Escaping characters not enclosed by quotes:

  • $ needs to be escaped with `$
  • @ needs to be escaped with `@
  • # needs to be escaped with `#
  • ` needs to be escaped with ``
  • | needs to be escaped with `|
  • { and } need to be escaped with `{ and `}
  • ; needs to be escaped with `;
  • " needs to be escaped with `"
  • ' needs to be escaped with `'

Exception:
A trailing (at the end of the double quote) $ doesn't need to be escaped at all.

Special always escaped characters:

  • `0: Null-Character
  • `a: Alert-Character
  • `b: Backspace-Character
  • `f: Form-Feed-Character
  • `n: New-Line-Character
  • `r: Carriage-Return-Character
  • `t: Horizontal-Tab-Character
  • `v: Vertical-Tab-Character

Special ways to stop parsing special characters:

  • Since Powershell 2.0 you can use @' ... '@ to prevent the interpretation of your parameters, but @' needs to be in the same line as your command, the parameters need to be in the next line and '@ needs to be in it's own line after the parameters
  • Since Powershell 3.0 you can use --% to stop parsing everything after --%

Example:

$h@h#h`h|h{h}h;h"h'h$:

  • Enclosed by double quotes: exampleprogram.exe "`$h@h#h``h|h{h}h;h`"h'h$"
  • Enclosed by single quotes: exampleprogram.exe '$h@h#h`h|h{h}h;h"h''h'$
  • Not enclosed by quotes: exampleprogram.exe `$h`@h`#h``h`|h`{h`}h`;h`"h`'h$
  • Using @' ... '@:

exampleprogram.exe @'
$h@h#h`h|h{h}h;h"h'h$
'@

  • Using --%: exampleprogram.exe --% $h@h#h`h|h{h}h;h"h'h$
testeaxeax
  • 1,535