1

I have a program which calls a script that outputs progress information in line-break separated output with a prefix in JSON on file descriptor number 3:

use std::process::Command;

fn main() {
    let out = Command::new("bash")
        .arg("test.sh")
        .output()
        .expect("failed to execute install command");
}
#!/bin/bash

while sleep 1s; do # random progress emulator
  echo "@progress {\"key\": \"value\"}" >&3
done

Nothing I've found provided any good example of how to actually to this.

The JavaScript equivalent would be:

#!/usr/bin/env node

const cp = require('child_process')

const p = cp.spawn('bash', [ 'test.sh' ],  { stdio: [ 'pipe', 'inherit', 'inherit', /* 3 */ 'pipe' ] })

const readline = require('readline')

const rl = readline.createInterface({
  input: p.stdio[3]
})

rl.on('line', (line) => console.log(JSON.parse(line.substr(10))))
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
mkg20001
  • 171
  • 1
  • 10
  • 1
    Does this answer your question? [How can I read from a specific raw file descriptor in Rust?](https://stackoverflow.com/questions/27665396/how-can-i-read-from-a-specific-raw-file-descriptor-in-rust) – Joseph Sible-Reinstate Monica May 02 '20 at 22:12
  • @JosephSible-ReinstateMonica this talks about how to read/write to specific fd's of the current process. I need somethign that talks about reading/writing of specific fd's of the child, which is only vaguely answered. I just can't connect those two without a decent example. – mkg20001 May 03 '20 at 10:20
  • 1
    Child processes start with the same FD's as their parent unless you specify otherwise, and your code does not specify otherwise for that FD. – Joseph Sible-Reinstate Monica May 03 '20 at 19:39
  • So how do I spawn the process to have a third fd? – mkg20001 May 04 '20 at 11:12
  • I added a javascript version. I basically want to make the same thing in rust. – mkg20001 May 04 '20 at 11:25
  • I don't see anything allowing me to attach a 3 fd, this only handles stdin, out, err: https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/process/process_unix.rs#L353-L373 – mkg20001 May 04 '20 at 11:57

0 Answers0