我想传递node.js的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 "/home/kali/Desktop/output"作为一个命令。但是它会执行"echo hello", "world", "to"one_answers"all"命令,所以我有错误。由于我的项目的一些限制,我不能使用双引号和引号内反引号。我该怎么办?
编辑:
new line是execSync的分隔符。是否有办法改变在execSync的分隔符?或者是否有任何替代execSync,其分隔符不是新行?
您应该能够使用replace
来删除换行符。require('child_process').execSync(`echo hello
world
to
all > /home/kali/Desktop/output`.replace(/(rn|n|r)/gm, ``));
相当于运行
require('child_process').execSync(`echo hello world to all > /home/kali/Desktop/output`);