package main
import (
"os/exec"
)
func main() {
cmd:= exec.Command("cd", "/")
cmd.Run()
}
这样试着执行cd命令但是不行。问一下如何执行cd命令进入目录
更多评论
方法1:
func ExcuteShell(commond string) bool {
c := commond
var cmd *exec.Cmd
cmd = exec.Command("cmd.exe", "/c", c)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return false
}
return true
}
方法2:
func stopExecCommand() bool {
// 执行的命令cmd和/C是不能少的,命令如遇空格就必须多加一个参数,例如以下
cmd := exec.Command("cmd","/C", "net","stop","Tomcat7","/y")
//显示运行的命令
fmt.print(cmd.Args)
stdout, err := cmd.StdoutPipe()
if err != nil {
return false
}
cmd.Start()
reader := bufio.NewReader(stdout)
//实时循环读取输出流中的一行内容
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
}
}
cmd.Wait()
return true
}
#1