-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
This program should work, but fails
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
)
func main() {
var prog string
if runtime.GOOS != "windows" {
prog = "date"
// add . at the front of our PATH,
// windows starts search in the current directory anyway
p := os.Getenv("PATH")
err := os.Setenv("PATH", ".:"+p)
if err != nil {
log.Fatal(err)
}
} else {
prog = "ipconfig"
}
// find our executable
path, err := exec.LookPath(prog)
if err != nil {
log.Fatal(err)
}
dir, _ := filepath.Split(path)
// chdir to where our executable lives
err = os.Chdir(dir)
if err != nil {
log.Fatal(err)
}
// find our executable again
path, err = exec.LookPath(prog)
if err != nil {
log.Fatal(err)
}
// run our executable with /tmp directory as current
cmd := exec.Command(path)
cmd.Dir = os.TempDir()
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Printf("out=%v\n", string(out))
}
I am not sure where things should be changed.
Alex