Bird
0
0

You want to run a custom executable ./script.sh with arguments arg1 and arg2 using execFile. Which code snippet correctly runs it and logs both output and errors safely?

hard📝 Application Q15 of 15
Node.js - Child Processes
You want to run a custom executable ./script.sh with arguments arg1 and arg2 using execFile. Which code snippet correctly runs it and logs both output and errors safely?
AexecFile('./script.sh', ['arg1', 'arg2'], (error, stdout, stderr) => { if (error) { console.error('Execution error:', error); return; } if (stderr) { console.error('Error output:', stderr); } console.log('Program output:', stdout); });
BexecFile('./script.sh arg1 arg2', (error, stdout) => { if (error) throw error; console.log(stdout); });
CexecFile('./script.sh', (error, stdout, stderr) => { console.log(stdout); console.log(stderr); });
DexecFile(['./script.sh', 'arg1', 'arg2'], (error, stdout, stderr) => { if (error) throw error; console.log(stdout); });
Step-by-Step Solution
Solution:
  1. Step 1: Check argument passing

    Arguments must be passed as an array separate from the executable path, so ['arg1', 'arg2'] is correct.
  2. Step 2: Verify error and output handling

    execFile('./script.sh', ['arg1', 'arg2'], (error, stdout, stderr) => { if (error) { console.error('Execution error:', error); return; } if (stderr) { console.error('Error output:', stderr); } console.log('Program output:', stdout); }); checks for execution errors, logs stderr if present, and prints stdout, which is safe and complete.
  3. Final Answer:

    execFile('./script.sh', ['arg1', 'arg2'], (error, stdout, stderr) => { ... }) -> Option A
  4. Quick Check:

    Pass args array and handle error, stderr, stdout = C [OK]
Quick Trick: Pass args as array, handle error and stderr separately [OK]
Common Mistakes:
  • Passing all args in one string instead of array
  • Ignoring stderr output
  • Passing args as first parameter array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes