// exec.gopackagemainimport ("fmt""os/exec""os")funcmain() {// 1) os.StartProcess ///*********************//* Linux: */env := os.Environ()procAttr :=&os.ProcAttr{ Env: env, Files: []*os.File{ os.Stdin, os.Stdout, os.Stderr, }, }// 1st example: list filespid, err := os.StartProcess("/bin/ls", []string{"ls", "-l"}, procAttr) if err !=nil { fmt.Printf("Error %v starting process!", err) // os.Exit(1)}fmt.Printf("The process id is %v", pid)
输出:
The process id is &{20540}total 2056-rwxr-xr-x 1 ivo ivo 11575552011-07-0416:48 Mieken_exec-rw-r--r--1 ivo ivo 21242011-07-0416:48 Mieken_exec.go-rw-r--r--1 ivo ivo 185282011-07-0416:48 Mieken_exec_go_.6-rwxr-xr-x 1 ivo ivo 9139202011-06-0316:13 panic.exe-rw-r--r--1 ivo ivo 1802011-04-1120:39 panic.go
// 2nd example: show all processespid, err = os.StartProcess("/bin/ps", []string{"-e", "-opid,ppid,comm"}, procAttr) if err !=nil { fmt.Printf("Error %v starting process!", err) // os.Exit(1)}fmt.Printf("The process id is %v", pid)
// 2) exec.Run ///***************/// Linux: OK, but not for ls ?// cmd := exec.Command("ls", "-l") // no error, but doesn't show anything ?// cmd := exec.Command("ls") // no error, but doesn't show anything ? cmd := exec.Command("gedit") // this opens a gedit-window err = cmd.Run()if err !=nil { fmt.Printf("Error %v executing command!", err) os.Exit(1) } fmt.Printf("The command is %v", cmd)// The command is &{/bin/ls [ls -l] [] <nil> <nil> <nil> 0xf840000210 <nil> true [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [] [] 0xf8400128c0}
}// in Windows: uitvoering: Error fork/exec /bin/ls: The system cannot find the path specified. starting process!