Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Created April 5, 2017 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnilchee/fbbb3b82962f8823979af891cfe624c6 to your computer and use it in GitHub Desktop.
Save gnilchee/fbbb3b82962f8823979af891cfe624c6 to your computer and use it in GitHub Desktop.
Take command line args and execute in shell
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
var cmdName string = os.Args[1]
var cmdArgs []string = os.Args[2:]
cmdOut, err := exec.Command(cmdName, cmdArgs...).Output()
if err != nil {
fmt.Fprintln(os.Stderr, "There was an error running command: ", err)
os.Exit(1)
}
output := string(cmdOut)
fmt.Println(output)
}
# ./exec_cmd ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:05 ? 00:00:00 bash
root 29792 1 0 04:35 ? 00:00:00 ./exec_cmd ps -ef
root 29797 29792 0 04:35 ? 00:00:00 ps -ef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment