dan*_*ler 5 javascript bash shell node.js
我正在使用nodejs应用程序,我需要将多行字符串传递给shell命令.我不是shell脚本的专家,但是如果我在我的终端中运行这个命令就可以了:
$((cat $filePath) | dayone new)
这是我为nodejs方面所做的.dayone命令确实有效,但没有任何信息传输到它.
const cp = require('child_process');
const terminal = cp.spawn('bash');
var multiLineVariable = 'Multi\nline\nstring';
terminal.stdin.write('mul');
cp.exec('dayone new', (error, stdout, stderr) => {
console.log(error, stdout, stderr);
});
terminal.stdin.end();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
在这里,您使用spawn 启动bash,然后使用exec 启动dayone 程序。它们是单独的子进程并且不以任何方式连接。
'cp'只是对child_process模块的引用,spawn和exec只是启动子进程的两种不同方式。
您可以使用 bash 并将 dayone 命令写入 stdin 以调用 dayone (正如您的代码片段似乎正在尝试执行的那样),或者您可以直接使用 exec 调用 dayone (请记住 exec 仍然在 shell 中运行该命令) :
var multiLineVariable = 'Multi\nline\nstring';
// get the child_process module
const cp = require('child_process');
// open a child process
var process = cp.exec('dayone new', (error, stdout, stderr) => {
console.log(error, stdout, stderr);
});
// write your multiline variable to the child process
process.stdin.write(multiLineVariable);
process.stdin.end();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1317 次 |
| 最近记录: |