[英]How to use double quotes inside double quotes
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). 我收到语法错误( "
printf
在这里有错误)。
I tried to escape using \\"
, to no avail. 我试图逃避使用\\"
,无济于事。
Using: "df -l | awk '{t += $2} END {printf \\"%d G\\n\\", t / 2^20 + 0.5}'"
. 使用: "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? 在这里进行语法正确的方法是什么?
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
. 问题在于\\n
。 It should be \\\\n
. 它应该是\\\\n
。 You can debug any shell task like this: console.log(SHELL_COMMAND)
and then mannually run output string. 您可以调试任何shell任务,如: console.log(SHELL_COMMAND)
,然后手动运行输出字符串。
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. 这毫无意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.