Running a bash script as a subprocess in Node.js

    0

    0

    Prince Igwe

    This code will execute the ./<path> bashscript.sh command, and will print the following output:

    stdout:

    ./test.sh: line 1: syntax error near `;' ./test.sh: line 2: unexpected ')'

    stderr:

    error:

    ./test.sh: line 1: syntax error near `;' ./test.

    import {exec} from 'node:child_process'
    
    exec('sh ./<path /to /the /bashcript.sh> ', (error, stdout, stderr) => {
      if(error) {
        console.log(`error: ${error.message}`)
        return;
        }
      if(stderr) {
        console.error(`stderr: ${stderr}`)
        return
      }
      console.log(`stdout: \n${stdout}`)
      })
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.