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

Capture and play voice with Asterisk ARI -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -

visual studio - Installing Packages through Nuget - "Central Directory corrupt" -