使用exec.Command()执行shell命令

package main

import (
   "fmt"
   "os/exec"
)

func main() {
   cmd := exec.Command("/bin/bash", "-c", "whoami") // 执行whoami命令
   output, err := cmd.CombinedOutput()
   if err != nil {
      panic(err)
   }

   fmt.Println(string(output)) // root
}

Copyright © 2024 码农人生. All Rights Reserved