i am trying to get nodejs to execute a shell script programatically if a certain url is called.
i can call single line unix commands like "echo hello-world". and this behaves how i expect (output to stdout "hello-world").
import { exec } from 'child-process';
exec('echo hello-world', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
console.log('Deployment finished!');
return stdout;
})
but i want to run a script with this command. e.g.
shell-script.sh (which is made executable chmod +x shell-script.sh
)
echo hello-world
ls -la
and nodejs:
exec('./shell-script.sh', ...)
but this does not work in nodejs. i tried the same script in my desktop terminal and it works fine.
i then test the same with only the first line. (echo hello-world
), which worked but i dont know why.
i then modified my shell script to:
echo hello-world &&\
ls -la
note: i used &&\
to make the command into one line when executed.
this worked! but is there a way to execute a shell script with multiples lines without having to add &&\
at the end of each line?