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:

  1. The program demonstrates running various shell commands (date, date -x, grep hello, and ls -a -l -h) using the exec.Command function.

  2. The Output method is used to capture the standard output of the commands.

  3. For the grep hello command, the program demonstrates how to provide input to the command using StdinPipe.

  4. The program also shows how to handle errors and exit codes of the executed commands.

  5. The last command runs ls -a -l -h using bash -c to 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