javascript - How to use double quotes inside double quotes -


consider following:

var child = require('child_process'); var res = child.execsync("df -l | awk '{t += $2} end {printf "%d g\n", t / 2^20 + 0.5}'"); 

i'm getting syntax error (the " in printf @ fault here).

i tried escape using \", no avail.

using: "df -l | awk '{t += $2} end {printf \"%d g\n\", t / 2^20 + 0.5}'".

i get:

awk: cmd. line:1: {t += $2} end {printf "%d g awk: cmd. line:1:                       ^ unterminated string 

what syntactically correct way proceed here?

var res = child.execsync("df -l | awk \'{t += $2} end {printf \"%d g\\n\", t / 2^20 + 0.5}\'");

works, tested.

the problem \n. should \\n. can debug shell task this: console.log(shell_command) , mannually run output string.

for example, this:

var child = require('child_process'); var cmd = "df -l | awk '{t += $2} end {printf \"%d g\n\", t / 2^20 + 0.5}'"; console.log(cmd) var res = child.execsync(cmd); 

will output in console , try run this:

df -l | awk '{t += $2} end {printf "%d g ", t / 2^20 + 0.5}' 

which makes no sense.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -