执行命令不获取结果
直接调用run()
package main
import (
"fmt"
"os/exec"
)
func main() {
//新建文件1.txt
cmd := exec.Command("cmd", "/C", "echo", "111111", ">", "1.txt").Run()
fmt.Println(cmd)
}
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd, _ := exec.Command("cmd", "/C", "echo", "123", ">", "123.txt").CombinedOutput()
fmt.Println(string(cmd))
}
如果是Linux就简单了,不用cmd /C。
执行命令并获取结果
使用CombinedOutput函数。
package main
import (
"fmt"
"os/exec"
)
func main() {
//新建文件1.txt
cmd, _ := exec.Command("cmd", "/C", "echo", "222222").CombinedOutput()
fmt.Println(string(cmd))
}