Simple exec.Command() program

359 views
Skip to first unread message

buc...@gmail.com

unread,
Nov 12, 2017, 10:35:00 PM11/12/17
to golang-nuts
package main

import(
"fmt"
"os/exec"
)

func main ()
cmd := exec.Command("ls", ">", "/dev/null")
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(output))
}


When I run this go program I get:
exit status: 1: ls: >:No such file or directory
/dev/null

When I do this from the (bash/sh) shell command line:
|# ls > /dev/null
it works correctly (no errors and exit status: 0


I've stared at this code for several days now and am getting nowhere.  Any thoughts?

andrey mirtchovski

unread,
Nov 12, 2017, 10:45:39 PM11/12/17
to buc...@gmail.com, golang-nuts
">" is a special character interpreted by your shell.
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Caleb Spare

unread,
Nov 12, 2017, 10:48:08 PM11/12/17
to buc...@gmail.com, golang-nuts
ls is a bash builtin. You can probably invoke the ls command with /bin/ls.

But also, passing ">" as an argument to exec.Command doesn't do what
you want. > is a redirection for a shell and exec.Command is not a
shell.

I'm not sure what you're trying to accomplish, but here are two things
you could do that may be helpful:

(1) If you just do exec.Command("ls"), cmd.Stdout is automatically
discarded (like doing '> /dev/null' in your shell). See the docs for
exec.Cmd.Stdout:

// If either is nil, Run connects the corresponding file descriptor
// to the null device (os.DevNull).

(2) If you want to invoke a shell, you can do something like
exec.Command("/bin/bash", "-c", "ls > /dev/null"). (I'm not sure what
the point of that particular invocation would be, though.)

Dan Kortschak

unread,
Nov 12, 2017, 10:51:26 PM11/12/17
to buc...@gmail.com, golang-nuts
exec.Command does not invoke a shell, the `>' you use in bash is a
shell function (https://www.gnu.org/software/bash/manual/bash.html#Redi
rections). Note that the example for CombinedOutput invokes "sh" as the
command with "-c", "ls... (https://golang.org/pkg/os/exec/#example_Cmd_
CombinedOutput).

Nilsocket

unread,
Nov 13, 2017, 12:47:56 AM11/13/17
to golang-nuts
package main

import (
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("ls")
    null, _ := os.Open(os.DevNull)
    defer null.Close()
    cmd.Stdout = null
    cmd.Run()
}

What you were trying to do is, redirecting output of ls to /dev/null.
`ls` is the name of the command.
`>` is redirection operator.
`/dev/null` is a device or a file to which you are redirecting your output.
In your case `ls` doesn't have any arguments so, we don't provide any.
Inorder to write to `/dev/null`, we need to open it first.
Then we were redirecting cmd.Stdout to null.
(i.e., instead of writing commands ouput stdout, it will write to /dev/null)
cmd.Run() //runs the command.

buc...@gmail.com

unread,
Nov 13, 2017, 12:39:28 PM11/13/17
to golang-nuts
You guys are great.  Not only did you explain at length, what I was misunderstanding,  but you offered several solutions.

Thanks to one and all!
 
Reply all
Reply to author
Forward
0 new messages