Spawning Processes
This code is a Go program that demonstrates executing shell commands from Go using the os/exec package. Here's a breakdown of the code:
package main
import (
"fmt"
"io"
"os/exec"
)
func main() {
// Command: date
dateCmd := exec.Command("date")
dateOut, err := dateCmd.Output()
if err != nil {
panic(err)
}
fmt.Println("> date")
fmt.Println(string(dateOut))
// Command: date -x (intentionally invalid command)
_, err = exec.Command("date", "-x").Output()
if err != nil {
switch e := err.(type) {
case *exec.Error:
fmt.Println("failed executing:", err)
case *exec.ExitError:
fmt.Println("command exit rc =", e.ExitCode())
default:
panic(err)
}
}
// Command: grep hello
grepCmd := exec.Command("grep", "hello")
grepIn, _ := grepCmd.StdinPipe()
grepOut, _ := grepCmd.StdoutPipe()
grepCmd.Start()
grepIn.Write([]byte("hello grep\ngoodbye grep"))
grepIn.Close()
grepBytes, _ := io.ReadAll(grepOut)
grepCmd.Wait()
fmt.Println("> grep hello")
fmt.Println(string(grepBytes))
// Command: ls -a -l -h
lsCmd := exec.Command("bash", "-c", "ls -a -l -h")
lsOut, err := lsCmd.Output()
if err != nil {
panic(err)
}
fmt.Println("> ls -a -l -h")
fmt.Println(string(lsOut))
}Explanation:
The program demonstrates running various shell commands (
date,date -x,grep hello, andls -a -l -h) using theexec.Commandfunction.The
Outputmethod is used to capture the standard output of the commands.For the
grep hellocommand, the program demonstrates how to provide input to the command usingStdinPipe.The program also shows how to handle errors and exit codes of the executed commands.
The last command runs
ls -a -l -husingbash -cto execute a shell command with arguments.
Please note that executing arbitrary commands obtained from user input can pose security risks (command injection). Always validate and sanitize user inputs before using them in command execution.
Last updated
Was this helpful?