GeezerVison is showing github.com — spawnSync shell command, error cut: -: Bad file descriptor · Issue #2284 · nodejs/help
spawnSync shell command, error cut: -: Bad file descriptor #2284DescriptionHere is my stackoverflow link to the question. https://stackoverflow.com/questions/58796378/spawnsync-for-shell-commands
I wonder if this may be a bug?
const { spawnSync } = require('child_process');
const fs = require('fs-extra');
function trimFile() {
let rfd;
let wfd;
try {
rfd = fs.openSync('filelist.txt', 'r');
wfd = fs.openSync('filesonly.txt', 'w');
spawnSync('cut -d" " -f1', { stdio: [wfd, rfd, 'inherit'] });
}
finally {
fs.closeSync(rfd);
fs.closeSync(wfd);
}
}
- Version:
11.8.0
- Platform:
Ubuntu 18.04
Linux computerName 4.15.0-66-generic Deprecate domains node#75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
- Subsystem:
child_process
ActivityTrott commented on Nov 12, 2019 It won't fix your problem, but FYI Node.js 11 is unsupported. Please consider moving to Node.js 12 or 13 instead. Trott commented on Nov 12, 2019 You need to separate your commands from the arguments in your spawnSync() invocation:
spawnSync('cut', ['-d', " ", '-f1'], { stdio: [wfd, rfd, 'inherit'] });
Otherwise, it treats the command as cut -d" " -f1 which you might think will work but there is unlikely to be a command cut -d" " -f1 on your system. It's cut with the other stuff as arguments. Trott commented on Nov 12, 2019 (It still won't do what you want, I don't think, but that's one thing that needs fixing.) Tatsujinichi commented on Nov 21, 2019 Oh I see. Is this correct with shell: true? github-actions commented on Jan 25, 2023 There has been no activity on this issue for 3 years and it may no longer be relevant. It will be closed 1 month after the last non-automated comment. andres-lowrie commented on Feb 10, 2023 so this may still be an issue perhaps?
With latest lts version
Given I have 2 files in some directory
and write a script like this
const { spawnSync } = require('child_process')
const fs = require('fs')
const out = fs.openSync('./out')
const err = fs.openSync('./err')
spawnSync('echo', ['hello'], { stdio: ['pipe', out, err]})
the script runs without errors but the file used for stdout is empty
ls -l
-rw-r--r-- 1 andreslowrie staff 0 Feb 10 16:37 err
-rw-r--r-- 1 andreslowrie staff 264 Feb 10 16:37 index.js
-rw-r--r-- 1 andreslowrie staff 0 Feb 10 16:37 out
Where I'm expecting there would be hello in the file out
If I instead use fs.createWriteStream
const { spawnSync } = require('child_process')
const fs = require('fs')
//const out = fs.openSync('./out')
//const err = fs.openSync('./err')
//
const out = fs.createWriteStream('./out')
const err = fs.createWriteStream('./err')
out.on('open', () => {
err.on('open', () => {
spawnSync('echo', ['hello'], { stdio: ['pipe', out, err] })
})
})
it in fact writes to the output
ls -l
-rw-r--r-- 1 andreslowrie staff 0 Feb 10 16:40 err
-rw-r--r-- 1 andreslowrie staff 352 Feb 10 16:40 index.js
-rw-r--r-- 1 andreslowrie staff 6 Feb 10 16:40 out
cat out
hello
This is probably a user error in reading the docs, but shouldn't the first method work the same way the second method does?, given that the script passes open fd numbers to the stdio option ? github-actions commented on May 14, 2024 It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links. github-actions commented on Jun 13, 2024 It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|