exec.Command("ls", "*.go") exit status 2

5,498 views
Skip to first unread message

Bailin Huang

unread,
Aug 19, 2013, 1:19:11 AM8/19/13
to golan...@googlegroups.com
    I wrote a piece of code as below:

    cmd := exec.Command("ls", "*.go")
    out, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("%s\n", out)

   However, when I run the program, it just print out :"exit status 2", I don't know why.
   I just want to list all of the files that has suffix ".go", but I got confused then that happen.

Ian Lance Taylor

unread,
Aug 19, 2013, 1:22:07 AM8/19/13
to Bailin Huang, golang-nuts
I recommend that you change Output to CombinedOutput and print the
variable "out" when err != nil. That will probably tell you what is
happening, as it will show you the standard error of the ls command.

Ian

Bailin Huang

unread,
Aug 19, 2013, 1:28:25 AM8/19/13
to golan...@googlegroups.com, Bailin Huang
When I use CombinedOutput, it shows:
> ls: cannot access *.go: No such file or directory
When I run `ls *.go` on shell, it works as desired, it shows all files that ends with `.go`.
But why this command on exec just output `No such file or directory` ?

在 2013年8月19日星期一UTC+8下午1时22分07秒,Ian Lance Taylor写道:

Dan Kortschak

unread,
Aug 19, 2013, 1:29:39 AM8/19/13
to Bailin Huang, golan...@googlegroups.com
`*' is a shell globbing character. exec.Command doesn't operate through
a shell. You have effectively executed `ls \*.go'.

Bailin Huang

unread,
Aug 19, 2013, 1:31:09 AM8/19/13
to golan...@googlegroups.com, Bailin Huang
BTW, if I want to run command like `echo "what\nhappen\n" | grep 'what' | wc -l` using exec, is there any convenient way to do that ?


在 2013年8月19日星期一UTC+8下午1时22分07秒,Ian Lance Taylor写道:
On Sun, Aug 18, 2013 at 10:19 PM, Bailin Huang <bolin...@gmail.com> wrote:

andrey mirtchovski

unread,
Aug 19, 2013, 1:35:18 AM8/19/13
to Bailin Huang, golang-nuts
> BTW, if I want to run command like `echo "what\nhappen\n" | grep 'what' | wc
> -l` using exec, is there any convenient way to do that ?

if you want to have shell interpreting in your go-executed commands
you're better off using the shell to interpret things. for example, if
you had done this you would have had globbing working right off the
bat:

cmd := exec.Command("/bin/bash", "-c", "ls", "*.go")

Dave Cheney

unread,
Aug 19, 2013, 1:40:11 AM8/19/13
to Bailin Huang, golang-nuts
You may want to try

exec.Command("bash", "-c", "ls *.go") // bash -c 'ls *.go'

again, check the output of the CombinedOutput and the error code.

This command, even when run through bash will fail if there are no .go
files in the _current working directory_. This last part is important,
don't assume that your Go program is being run in the current shell
working directory, especially when using go run.
> --
> 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/groups/opt_out.

Bailin Huang

unread,
Aug 19, 2013, 1:40:53 AM8/19/13
to golan...@googlegroups.com, Bailin Huang
Thanks, I suddenly realize that exec is not shell, '*' is not supported in exec.

在 2013年8月19日星期一UTC+8下午1时35分18秒,andrey mirtchovski写道:

luz...@gmail.com

unread,
Aug 19, 2013, 3:33:18 AM8/19/13
to golan...@googlegroups.com, Bailin Huang
On Monday, August 19, 2013 7:31:09 AM UTC+2, Bailin Huang wrote:
BTW, if I want to run command like `echo "what\nhappen\n" | grep 'what' | wc -l` using exec, is there any convenient way to do that ?

You can either delegate to bash as others suggested or use this pipe package: http://labix.org/pipe
Reply all
Reply to author
Forward
0 new messages