nodejs child_process spawn 사용
조회수 1874회
다음과 같은 코드를 nodejs에서 실행하고 싶습니다.
$ gcc test.c
$ ./a.out < inputfile.txt
그래서 다음과 같은 코드를 작성했는데요.
var gpp = spawn('g++', ['test.cpp']);
var run = spawn('./a.out', ['< input.txt']);
var run = spawn('./a.out < input.txt']);
컴파일까지는 잘 되었으나 실행 부분에서
위와 같이 시도해보았으나 작동하지 않았습니다..
제가 실행하고 싶은 것은 a.out
에 input.txt
를 입력하는 것인데
생각대로 되지 않아서 질문을 드립니다.
어떻게 하면 되나요?
또 한가지 더 질문드리자면
공식 문서를 참고해서 하고 있는데요
child_process.spawn(command[, args][, options])
이 부분에서 args
가 정확하게 무엇을 넣어야하는 건지 알고 싶습니다.
1 답변
-
자답입니다.
같은 내용을 스텍오버플로우에도 질문했는데요.
아래와 같은 답변을 받았습니다.
Since
<
(redirection) is a shell construct, you need to run your command line with a shell. That's whatchild_process.exec()
is for:const exec = require('child_process').exec; exec('./a.out < inputfile.txt', function(err, stdout, stderr) { if (err) { return console.error('exec error:', err); } console.log('stdout:', stdout); console.log('stderr:', stderr); });
댓글 입력