2

What is the difference between cmd1 > tmp && cmd2 < tmp and cmd1 | cmd2, besides the creation of a tmp file (obviously) ?

If my comprehension of pipe is right, I expect cmd2 to read from standard input the output of cmd1 in both cases, so I was expecting the same behaviour from those two.

However with this go program that reads standard input, it works as intended with pipe, but with the second form, it acts as if no input were received.

4

The Go program appears to explicitly test whether standard input is a pipe, and if it is not, it exits with a diagnostic message:

fi, err := os.Stdin.Stat()
if err != nil {
        fatal(err)
}
if fi.Mode()&os.ModeNamedPipe == 0 {
        usage()
        os.Exit(1)
}

The other difference between the two command lines is that with the pipe, the two commands are run simultaneously. The left hand side command is writing and the right hand side command is reading. If one is too slow for the other (in terms of reading/writing), the faster program will temporarily block until the slower catches up.

In the case where the two commands are run with && in-between them, they are obviously run after each other. The second command will not start until the first command has finished and has terminated with a zero exit status.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.