How to pass parameters to bash command in node exec function?
Asked Answered
I

2

6

I want to run bash script with params in node js child_process.exec()

In doc is String The command to run, with space-separated arguments, but

child.exec("cat path_to_file1 path_to_file2") 

doesn't work. It is internally changed to

/bin/sh -c cat path_to_file1 path_to_file2

which fails. How can I run this correctly? In shell I would fix it this way

/bin/sh -c 'cat path_to_file1 path_to_file2'

(I didn't write callbacks, because I ask only about command param)

Indisposition answered 17/12, 2015 at 14:51 Comment(1)
Not sure I understand your question. exec("cat file1 file2") works correctly.Chud
I
3

Use the shell option of exec :

child.exec("cat path_to_file1 path_to_file2", {
 shell : '/bin/bash' 
})

see options : https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Innocence answered 17/12, 2015 at 15:8 Comment(1)
Yes, but can be updated . So, you force the shell with the shell option .Innocence
A
-1

Did you tried something like this?

var exec = require('child_process').exec;
var child;
child = exec("cat '/route/to/file_a' '/route/to/file_b'", function (error, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});

That way you can find what is wrong or get the correct output. ;)

Affront answered 17/12, 2015 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.