字符串传递给execsync有新行,我想将字符串作为单个命令执行

发布于 2025-02-09 13:29:31 字数 1811 浏览 1 评论 0原文

我想通过

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

围归者 2025-02-16 13:29:31

您应该能够用替换

require('child_process').execSync(`echo hello 
world 
to
all > /home/kali/Desktop/output`.replace(/(\r\n|\n|\r)/gm, ``));

等同于运行

require('child_process').execSync(`echo hello world to all > /home/kali/Desktop/output`);

you should be able to remove the line breaks with a replace

require('child_process').execSync(`echo hello 
world 
to
all > /home/kali/Desktop/output`.replace(/(\r\n|\n|\r)/gm, ``));

which would be equivalent to running

require('child_process').execSync(`echo hello world to all > /home/kali/Desktop/output`);
裂开嘴轻声笑有多痛 2025-02-16 13:29:31

您可以像在终端内部一样使用后斜线:

const command = `yourcommand \
    --option1 value \
    --option2 \
    --option3 "${variable}"
`;

require('child_process').execSync(command);

You can use backslash, as you can do inside a terminal :

const command = `yourcommand \
    --option1 value \
    --option2 \
    --option3 "${variable}"
`;

require('child_process').execSync(command);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文