exec.Command sample code for show the stderr too

2,037 views
Skip to first unread message

dlin

unread,
Oct 26, 2011, 3:21:44 AM10/26/11
to golan...@googlegroups.com
I've searched this group.  But, there are old samples.  And as I know exec.Command now return one argument.

When I execute "gnuplot tmp.gnuplot" on bash, it returns:

"tmp.gnuplot", line 25: warning: Skipping unreadable file "$1"
"tmp.gnuplot", line 25: warning: Skipping unreadable file "$1"
"tmp.gnuplot", line 25: No data in plot

But, when I execute my code, it returns:

2011/10/26 15:17:35 exit status 1

my code
=======
func plot() {
scriptName := "tmp.gnuplot"
cmd := exec.Command("gnuplot", scriptName)
bs, err := cmd.Output()
if err != nil { log.Fatalln(err) }
fmt.Println(bs)
}

roger peppe

unread,
Oct 26, 2011, 4:12:51 AM10/26/11
to golan...@googlegroups.com
On 26 October 2011 08:21, dlin <dli...@gmail.com> wrote:
> I've searched this group.  But, there are old samples.  And as I know
> exec.Command now return one argument.
> When I execute "gnuplot tmp.gnuplot" on bash, it returns:
> "tmp.gnuplot", line 25: warning: Skipping unreadable file "$1"
> "tmp.gnuplot", line 25: warning: Skipping unreadable file "$1"
> "tmp.gnuplot", line 25: No data in plot
> But, when I execute my code, it returns:
> 2011/10/26 15:17:35 exit status 1

i think you probably want to use CombinedOutput rather
than Output - the messages that gnuplot is printing
are being sent to stderr, which is directed to /dev/null by
default.

Sebastien Binet

unread,
Oct 26, 2011, 5:15:51 AM10/26/11
to dlin, golan...@googlegroups.com

see:
https://bitbucket.org/binet/go-gnuplot/src/ba2c342cf191/pkg/gnuplot/gnuplot.go#cl-51

my simple minded attempt at providing gnuplot commands from go.
I accept patches and push requests :)

-s

dlin

unread,
Oct 26, 2011, 5:35:58 AM10/26/11
to golan...@googlegroups.com, dlin
Sebastien, thanks.

I read the tutorial and  works.

Robert Bloomquist

unread,
Oct 28, 2011, 6:26:05 PM10/28/11
to golan...@googlegroups.com
Alternatively, you could connect gnuplot's standard error and standard output to the terminal's standard error and standard output:

func plot() {
scriptName := "tmp.gnuplot"
cmd := exec.Command("gnuplot", scriptName)
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { log.Fatalln(err) }
}

dlin

unread,
Oct 29, 2011, 2:35:19 AM10/29/11
to golan...@googlegroups.com
I'm wonder why CombinedOutput() failed, I think it may be a bug.

cmd := exec.Command("gnuplot", scriptname)

------------------
// workable code
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { log.Fatalln(err) }

// FAIL code
bs, e := cmd.CombinedOutput()
if e != nil {
log.Fatalln(e)
}
------------------
fmt.Println(string(bs))


roger peppe

unread,
Oct 30, 2011, 10:04:18 AM10/30/11
to golan...@googlegroups.com
it would be helpful if you could post a complete (small) example
that fails for you, as well as the error message that it gives.
otherwise we're left guessing!

dlin

unread,
Oct 30, 2011, 11:41:41 AM10/30/11
to golan...@googlegroups.com
Sorry, I've posted, but maybe not clearer.

result list here:

$ ./ok 
Cannot open load file 'wrong_file_name'
line 0: util.c: No such file or directory
2011/10/30 23:38:36 exit status 1

$ ./fail 
2011/10/30 23:38:38 exit status 1

// ok.go
func main() {
cmd := exec.Command("gnuplot", "wrong_file_name")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
}

//fail.go
func main() {
cmd := exec.Command("gnuplot", "wrong_file_name")
bs, e := cmd.CombinedOutput()
if e != nil {
log.Fatalln(e)
}
fmt.Println(string(bs))
}

John Asmuth

unread,
Oct 30, 2011, 11:51:30 AM10/30/11
to golan...@googlegroups.com
What happens if you print bs before log.Fatalln()ing?

gnuplut might be returning an error code, which turns into an err that is not nil.

log.Fatal() exits the program and bs won't be printed.

dlin

unread,
Oct 30, 2011, 8:56:59 PM10/30/11
to golan...@googlegroups.com
Thanks, John, you solved my question.
Reply all
Reply to author
Forward
0 new messages