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.
plink -ssh -l $user -batch -pw $pw $user@$IP "df -k / | tail -1 | awk '{print `$4}'"
"
as well.