Skip to content

Commit a5c9989

Browse files
committed
79: Executing commands using syscall & exec
1 parent 3dc094f commit a5c9989

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

execingprocesses/main.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
package execingprocesses
22

3-
// Similarly to spawning a subprocess
3+
import (
4+
"os"
5+
"os/exec"
6+
"syscall"
7+
)
8+
9+
// Similarly to spawning a subprocess, exec
10+
// can replace the go process with another one
11+
// As always, taking user input into exec directly
12+
// is a serious security flaw and should be used with caution.
413
func Run() {
14+
listFilesAndDirectories()
15+
}
16+
17+
func listFilesAndDirectories() {
18+
executable, err := exec.LookPath("ls")
19+
if err != nil {
20+
// POSIX only here again.
21+
panic(err)
22+
}
23+
args := []string{"ls", "-a", "-l", "-h"}
24+
env := os.Environ()
25+
if err := syscall.Exec(executable, args, env); err != nil {
26+
panic(err)
27+
}
528

629
}

0 commit comments

Comments
 (0)