Created
April 5, 2017 04:35
-
-
Save gnilchee/fbbb3b82962f8823979af891cfe624c6 to your computer and use it in GitHub Desktop.
Take command line args and execute in shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ./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