Reputation: 21621
Consider the 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 a syntax error (the "
in printf
are at fault here).
I tried to escape using \"
, to 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 is the syntactically correct way to proceed here?
Upvotes: 3
Views: 1431
Reputation: 4128
var res = child.execSync("df -l | awk \'{t += $2} END {printf \"%d G\\n\", t / 2^20 + 0.5}\'");
works, tested.
The problem was with \n
. It should be \\n
. You can debug any shell task like this:
console.log(SHELL_COMMAND)
and then 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 and try to run this:
df -l | awk '{t += $2} END {printf "%d G
", t / 2^20 + 0.5}'
which makes no sense.
Upvotes: 7