lieuwe_berg
lieuwe_berg

Reputation: 473

Writing to exec.Cmd.StdinPipe() doesn't do anything

I'm creating a new command with exec, obtaining the Stdin io.WriteCloser and writing to it as follows:

cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
cmdInWriter, err := cmd.StdinPipe()

err = cmd.Start()

go func() {
    for {
        var c string
        _, err = fmt.Scanln(&c)
        written, err := io.WriteString(cmdInWriter, c)
        fmt.Println(written) // prints 4, if c is "help"
    }
}()

However, this doesn't seem to actually write to the program.

I tested it with another program I quickly made (the previous list does not apply there) and the string was again not being written. What am I doing wrong?

Upvotes: 0

Views: 777

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51567

You are missing the linefeed. When you read a string using Scanln, the resulting string does not have the linefeed at the end:

 written, err := fmt.Fprintln(cmdInWriter, c)

Upvotes: 2

Related Questions