If you specify shell = True
in subrocess.call
when executing a command from within a Python script, the string passed to args will be executed by the shell as it is.
According to Python documentation, / bin / sh
for Unix and COMSPEC
environment variables for Windows. Seems to be used as a shell, so if you want to do something similar in Go, would it look like this:
main.go
package main
import (
"os"
"os/exec"
"runtime"
)
func callSubprocess(cmdString string) (err error) {
osname := runtime.GOOS
var cmd *exec.Cmd
if osname == "windows" {
shell := os.Getenv("COMSPEC")
cmd = exec.Command(shell, "/c", cmdString)
} else {
shell := "/bin/sh"
cmd = exec.Command(shell, "-c", cmdString)
}
cmd.Stdout = os.Stdout
err = cmd.Run()
return
}
func main() {
err := callSubprocess("dir")
if err != nil {
os.Exit(1)
}
}
Recommended Posts