字符串传递给execsync有新行,我想将字符串作为单个命令执行
我想通过
require('child_process').execSync(`echo hello
world
to
all > /home/kali/Desktop/output`);
但是我有以下错误:
/bin/sh: 2: world: not found
node:child_process:903
throw err;
^
Error: Command failed: echo hello
world > /home/kali/Desktop/output
/bin/sh: 2: world: not found
at checkExecSyncError (node:child_process:826:11)
at Object.execSync (node:child_process:900:15)
at Object.<anonymous> (/home/kali/Desktop/vm_escape.js:36:26)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
status: 127,
signal: null,
output: [
null,
Buffer(6) [Uint8Array] [ 104, 101, 108, 108, 111, 10 ],
Buffer(29) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104,
58, 32, 50, 58, 32, 119, 111,
114, 108, 100, 58, 32, 110, 111,
116, 32, 102, 111, 117, 110, 100,
10
]
],
pid: 216244,
stdout: Buffer(6) [Uint8Array] [ 104, 101, 108, 108, 111, 10 ],
stderr: Buffer(29) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104,
58, 32, 50, 58, 32, 119, 111,
114, 108, 100, 58, 32, 110, 111,
116, 32, 102, 111, 117, 110, 100,
10
]
}
这是由于新行。它不会执行“ Echo Hello World to all&gt;/home/kali/desktop/output”作为单个命令。但是它执行“ Echo Hello”,“ World”,“”和“所有”命令,因此我有错误。由于项目的某些局限性,我无法使用背面报价的双引号和引号。我应该怎么办?
编辑:
New Line是Execsync的定界符。有什么方法可以更改execsync中的定界符?还是execsync有其他选择,其定界符不是新行?
I want to pass the execSync function of node.js a string that is not in a line (I mean it has a number of new lines). for example:
require('child_process').execSync(`echo hello
world
to
all > /home/kali/Desktop/output`);
but I have the below error:
/bin/sh: 2: world: not found
node:child_process:903
throw err;
^
Error: Command failed: echo hello
world > /home/kali/Desktop/output
/bin/sh: 2: world: not found
at checkExecSyncError (node:child_process:826:11)
at Object.execSync (node:child_process:900:15)
at Object.<anonymous> (/home/kali/Desktop/vm_escape.js:36:26)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
status: 127,
signal: null,
output: [
null,
Buffer(6) [Uint8Array] [ 104, 101, 108, 108, 111, 10 ],
Buffer(29) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104,
58, 32, 50, 58, 32, 119, 111,
114, 108, 100, 58, 32, 110, 111,
116, 32, 102, 111, 117, 110, 100,
10
]
],
pid: 216244,
stdout: Buffer(6) [Uint8Array] [ 104, 101, 108, 108, 111, 10 ],
stderr: Buffer(29) [Uint8Array] [
47, 98, 105, 110, 47, 115, 104,
58, 32, 50, 58, 32, 119, 111,
114, 108, 100, 58, 32, 110, 111,
116, 32, 102, 111, 117, 110, 100,
10
]
}
it is because of new lines. it doesn't execute "echo hello world to all > /home/kali/Desktop/output" as a single command. but It executes the "echo hello", "world", "to", and "all" commands so I have errors. because of some limitations in my project I can't use double quotation and quotation inside back quote. what should I do?
edited:
new line is the delimiter for execSync. is there any way to change the delimiter in execSync? or is there any alternative for execSync that its delimiter is not new line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够用替换
等同于运行
you should be able to remove the line breaks with a replace
which would be equivalent to running
您可以像在终端内部一样使用后斜线:
You can use backslash, as you can do inside a terminal :