FAQ
I am trying to print stdout and stderr of my test script in golang app
(sample.go) : ./test.sh | ./sample
test
#!/bin/bash
echo $$
i=0

while [ $i -lt 50 ]
do
     echo "$i time HELLO STDIN, $$!!!"
     echo "$i time HELLO STDERR, $$!!!" >&2
     sleep 1
     i=`expr $i + 1`
done

-------------------------
sample.go
------------------------
package main
import (
          "bufio"
          "fmt"
          "os"
         "os/signal"
  )


func main() {
         c := make(chan os.Signal, 1)
         signal.Notify(c, os.Interrupt)
         go func(){
                 for sig := range c {
         // sig is a ^C, handle it
                 fmt.Println(sig)
                 os.Exit(0)
            }
         }()
         consolereader := bufio.NewReader(os.Stdin)
         erreader := bufio.NewReader(os.Stderr)
         for {
                 input, err := consolereader.ReadString('\n')

                 if err != nil {
                         fmt.Println(err)
                         os.Exit(1)
                 }

                 errinput, err1 := erreader.ReadString('\n')
                 if err1 != nil {
                         fmt.Println(err1)
                         os.Exit(1)
                 }

                 fmt.Println(input)
                 fmt.Println(errinput)
                 //fmt.Println(err)
         }
         fmt.Println("\"reader finished\"");
}
I expected output to be like this
0 time HELLO STDOUT, 1077!!!
0 time HELLO STDERR, 1077!!!
....

But i see only STDERR:
0 time HELLO STDERR, 1077!!!
1 time HELLO STDERR, 1077!!!
Any idea what i do wrong in my code? Are there any examples on this
particular task?
Thank you,


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • James Bardin at Jan 22, 2015 at 9:50 pm
    this isn't really Go related...
    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com wrote:

    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console


    test
    #!/bin/bash
    echo $$
    i=0

    while [ $i -lt 50 ]
    do
    echo "$i time HELLO STDIN, $$!!!"
    echo "$i time HELLO STDERR, $$!!!" >&2
    sleep 1
    i=`expr $i + 1`
    done

    -------------------------
    sample.go
    ------------------------
    package main
    import (
    "bufio"
    "fmt"
    "os"
    "os/signal"
    )


    func main() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    go func(){
    for sig := range c {
    // sig is a ^C, handle it
    fmt.Println(sig)
    os.Exit(0)
    }
    }()
    consolereader := bufio.NewReader(os.Stdin)
    erreader := bufio.NewReader(os.Stderr)

    stderr isn't an input stream. There's nothing to read.


    for {
    input, err := consolereader.ReadString('\n')

    if err != nil {
    fmt.Println(err)
    os.Exit(1)
    }

    errinput, err1 := erreader.ReadString('\n')
    so this blocks forever, which is why you don't see any output.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Jan 22, 2015 at 10:11 pm
    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    ....
    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin wrote:

    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console


    test
    #!/bin/bash
    echo $$
    i=0

    while [ $i -lt 50 ]
    do
    echo "$i time HELLO STDIN, $$!!!"
    echo "$i time HELLO STDERR, $$!!!" >&2
    sleep 1
    i=`expr $i + 1`
    done

    -------------------------
    sample.go
    ------------------------
    package main
    import (
    "bufio"
    "fmt"
    "os"
    "os/signal"
    )


    func main() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    go func(){
    for sig := range c {
    // sig is a ^C, handle it
    fmt.Println(sig)
    os.Exit(0)
    }
    }()
    consolereader := bufio.NewReader(os.Stdin)
    erreader := bufio.NewReader(os.Stderr)

    stderr isn't an input stream. There's nothing to read.


    for {
    input, err := consolereader.ReadString('\n')

    if err != nil {
    fmt.Println(err)
    os.Exit(1)
    }

    errinput, err1 := erreader.ReadString('\n')
    so this blocks forever, which is why you don't see any output.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Jan 22, 2015 at 10:18 pm

    On Thu, Jan 22, 2015 at 5:11 PM, Victor L wrote:

    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    That's not coming from your go program.


    ....
    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin wrote:

    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console
    ^^ a shell pipe only connects one stream. The stderr is coming directly
    from your shell script.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Jan 22, 2015 at 10:35 pm
    what would be correct way of doing it? i suppose i can send stderr to
    stdout but how would my "go" code separate them?

    On Thu, Jan 22, 2015 at 5:18 PM, James Bardin wrote:


    On Thu, Jan 22, 2015 at 5:11 PM, Victor L wrote:

    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    That's not coming from your go program.


    ....
    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin wrote:

    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console
    ^^ a shell pipe only connects one stream. The stderr is coming directly
    from your shell script.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Jan 22, 2015 at 10:46 pm

    On Thu, Jan 22, 2015 at 5:35 PM, Victor L wrote:

    what would be correct way of doing it? i suppose i can send stderr to
    stdout but how would my "go" code separate them?
    One way would be to redirect stderr through a fifo.

    If inverting the executing is acceptable, you could use the os/exec
    package, and run the shell script from within the go program (
    http://golang.org/pkg/os/exec/). You can then read both the stdout and
    stderr directly from the child process.
    http://golang.org/pkg/os/exec/#Cmd.StderrPipe
    http://golang.org/pkg/os/exec/#Cmd.StdoutPipe

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Jan 22, 2015 at 11:16 pm
    That seems to be working fine, thanks...
    On Thu, Jan 22, 2015 at 5:46 PM, James Bardin wrote:

    On Thu, Jan 22, 2015 at 5:35 PM, Victor L wrote:

    what would be correct way of doing it? i suppose i can send stderr to
    stdout but how would my "go" code separate them?
    One way would be to redirect stderr through a fifo.

    If inverting the executing is acceptable, you could use the os/exec
    package, and run the shell script from within the go program (
    http://golang.org/pkg/os/exec/). You can then read both the stdout and
    stderr directly from the child process.
    http://golang.org/pkg/os/exec/#Cmd.StderrPipe
    http://golang.org/pkg/os/exec/#Cmd.StdoutPipe
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Jan 23, 2015 at 7:30 pm
    Would it work if i redirect like this : ./hello.sh >./sample 2>./sample
    ?
    On Thu, Jan 22, 2015 at 5:18 PM, James Bardin wrote:


    On Thu, Jan 22, 2015 at 5:11 PM, Victor L wrote:

    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    That's not coming from your go program.


    ....
    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin wrote:

    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console
    ^^ a shell pipe only connects one stream. The stderr is coming directly
    from your shell script.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Jan 23, 2015 at 8:08 pm
    No. You need the two to share the file descriptor. I wrote a book about
    this once; I recommend it.

    ./hello.sh >./sample 2>&1

    -rob

    On Fri, Jan 23, 2015 at 11:30 AM, Victor L wrote:

    Would it work if i redirect like this : ./hello.sh >./sample 2>./sample
    ?
    On Thu, Jan 22, 2015 at 5:18 PM, James Bardin wrote:


    On Thu, Jan 22, 2015 at 5:11 PM, Victor L wrote:

    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    That's not coming from your go program.


    ....

    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin <j.bardin@gmail.com>
    wrote:
    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang app
    (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console
    ^^ a shell pipe only connects one stream. The stderr is coming directly
    from your shell script.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Victor L at Jan 23, 2015 at 9:23 pm
    someone just gave me this example to do just that:
      ./hello.sh > >(cat > x) 2> >(cat > y)
    What's this "double redirect" do differently?
    On Fri, Jan 23, 2015 at 3:07 PM, Rob Pike wrote:

    No. You need the two to share the file descriptor. I wrote a book about
    this once; I recommend it.

    ./hello.sh >./sample 2>&1

    -rob

    On Fri, Jan 23, 2015 at 11:30 AM, Victor L wrote:

    Would it work if i redirect like this : ./hello.sh >./sample 2>./sample
    ?
    On Thu, Jan 22, 2015 at 5:18 PM, James Bardin wrote:


    On Thu, Jan 22, 2015 at 5:11 PM, Victor L wrote:

    errinput, err1 := erreader.ReadString('\n')
    If it blocks forever, why only STDERR messages coming through?
    The output i see never stops
    0 time HELLO STDERR, 963!!!
    1 time HELLO STDERR, 963!!!
    2 time HELLO STDERR, 963!!!
    That's not coming from your go program.


    ....

    On Thu, Jan 22, 2015 at 4:49 PM, James Bardin <j.bardin@gmail.com>
    wrote:
    this isn't really Go related...

    On Thursday, January 22, 2015 at 2:31:32 PM UTC-5, vlya...@gmail.com
    wrote:
    I am trying to print stdout and stderr of my test script in golang
    app (sample.go) : ./test.sh | ./sample
    You're only piping stdout. stderr is going to the console
    ^^ a shell pipe only connects one stream. The stderr is coming directly
    from your shell script.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Jan 23, 2015 at 9:41 pm

    On Fri, Jan 23, 2015 at 4:23 PM, Victor L wrote:

    someone just gave me this example to do just that:
    ./hello.sh > >(cat > x) 2> >(cat > y)
    What's this "double redirect" do differently?

    This is called "Process Substitution", and is giving you a unique FD for
    the stdin of each cat process. I don't see how it's going to solve your
    original problem, you could do that with i/o redirection by itself.

    This is also all unrelated to Go.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michael Jones at Jan 25, 2015 at 3:07 pm
    Lest there be any confusion, that book is:

    The UNIX Programming Environment
    by Brian W. Kernighan and Rob Pike
    http://www.amazon.com/Programming-Environment-Prentice-Hall-Software-Series/dp/013937681X

    From a well-considered review:

    "...*The Unix Programming Environment* is a book for developing your
    software mentality in a way that no other book that I've read even
    approaches. After 20 years as a Unix programmer, including kernel
    development of several Unix operating systems, this book still remains on
    my shelf."


    It here here by my desk as well. Excellent work by r & bwk. A year of their
    lives devoted to teach people the inner thinking of the tao.

    Michael
    On Fri, Jan 23, 2015 at 1:41 PM, James Bardin wrote:

    On Fri, Jan 23, 2015 at 4:23 PM, Victor L wrote:

    someone just gave me this example to do just that:
    ./hello.sh > >(cat > x) 2> >(cat > y)
    What's this "double redirect" do differently?

    This is called "Process Substitution", and is giving you a unique FD for
    the stdin of each cat process. I don't see how it's going to solve your
    original problem, you could do that with i/o redirection by itself.

    This is also all unrelated to Go.

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    *Michael T. Jones | Chief Technology Advocate, Google | mtj@google.com
    <mtj@google.com> | +1 650-335-5765*

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • James Bardin at Jan 23, 2015 at 8:54 pm

    On Fri, Jan 23, 2015 at 2:30 PM, Victor L wrote:

    Would it work if i redirect like this : ./hello.sh >./sample 2>./sample
    Not only what rob said, but in your fist examples, ./sample was your
    binary, so you just overwrote it.

    If you want to direct through a filesystem handle, you're better off making
    a fifo, and reading from that in your go program. The inconvenience there
    is that you need to create the fifo beforehand, and clean it up after.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 22, '15 at 8:30p
activeJan 25, '15 at 3:07p
posts13
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase