tags:

views:

144

answers:

2

Isn't this Golang program supposed to output a directory listing to stdout? It compiles ok, but does nothing.

package main

import "exec"

func main() {
  argv := []string{"-la"}
  envv := []string{}
  exec.Run("ls", argv, envv, "", exec.DevNull, exec.PassThrough, exec.MergeWithStdout)
}
A: 

exec.Run replaces your program with the one it executes -- it never returns to your app. This means that when 'cd' completes, it will exit as normal, and the only effect should be of changing the directory; 'ls' will never run.

Jorenko
I edited it and removed the first call to exec.Run.Still does nothing...
Sebastián Grignoli
Are you sure that it never returns to my app? Doesn't look like that in the documentation on exec.Run: Run starts the binary prog running with arguments argv and environment envv. It returns a pointer to a new Cmd representing the command or an error.
Sebastián Grignoli
Hrmph, I could have sworn that it was akin to execve et al.
Jorenko
+3  A: 

this works:

package main
import "exec"

func main() {
  cmd, err := exec.Run("/bin/ls", []string{"/bin/ls", "-la"}, []string{}, "", exec.DevNull, exec.PassThrough, exec.PassThrough)
  if (err != nil) {
    return
  }
  cmd.Close()
}
newacct
Conclusions:1) Looks like you have to use every declared variable, or else your program won't compile. |2) The first element in the argv array is not used by most of command line executables (it doesn´t really need to be "/bin/ls" here).
Sebastián Grignoli
3) calling ls only does nothing. You have to call /bin/ls. exec does not automtically lookup the program in the path.
Sebastián Grignoli