【Go】golangでbashを呼び出す

package main import ( "fmt" "os/exec" ) func main() { err := exec.Command("pwd").Run() if err != nil { fmt.Println("Command Exec Error.") } }
package main import ( "fmt" "os/exec" ) func main() { out, err := exec.Command("pwd").Output() if err != nil { fmt.Println("Command Exec Error.") } // 実行したコマンドの結果を出力 fmt.Printf("pwd result: %s", string(out)) }
- CombinedOutput package main import ( "fmt" "os/exec" ) func main() { out, err := exec.Command("ls", "dummy").CombinedOutput() if err != nil { fmt.Println("Command Exec Error.") } // 実行したコマンドの標準出力+標準エラー出力の内容 fmt.Printf("ls result: \n%s", string(out)) }
package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("sleep", "10") fmt.Println("Command Start.") err := cmd.Start() if err != nil { fmt.Println("Command Exec Error.") } //コメント外すと待機する //cmd.Wait() fmt.Println("Command Exit.") }