问个os.exec 执行windows 下命令的问题

16362361 · 2015-03-27 05:31:26 · 5364 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2015-03-27 05:31:26 的主题,其中的信息可能已经有所发展或是发生改变。

我在windows 目录下面有个cwRsync_5.4.1 的包

path="/cygdrive/c/null/ /cygdrive/c/data/20140101"

cmd := exec.Command("cmd", "/C", "C:\cwRsync_5.4.1\rsync.exe", "-av", "--delete", path) cmd := exec.Command("C:\cwRsync_5.4.1\rsync.exe", "-av", "--delete", path)

都执行无效果。

我拿cmd.Args() 的 里面的参数,在windows 下执行是好的。 另外我也试过了cmd.Start() cmd.Run() 都不可以。。。。。


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

5364 次点击  
加入收藏 微博
2 回复  |  直到 2015-03-27 07:20:10
wangfeiping
wangfeiping · #1 · 11年之前

我这样写的,可以执行,不知道是不是因为你参数设置方式不对?

func startService(serviceName string) {
    command := "sc"
    args := "start " + serviceName
    argArray := strings.Split(args, " ")
    cmd := exec.Command(command, argArray...)
    buf, err := cmd.Output()
    if err != nil {
        fmt.Fprintf(os.Stderr, "The command failed to perform: %s (Command: %s, Arguments: %s)", err, command, args)
        return
    }
    fmt.Fprintf(os.Stdout, "Result: %s", buf)
}
16362361
16362361 · #2 · 11年之前
wangfeipingwangfeiping #1 回复

我这样写的,可以执行,不知道是不是因为你参数设置方式不对? ```go func startService(serviceName string) { command := "sc" args := "start " + serviceName argArray := strings.Split(args, " ") cmd := exec.Command(command, argArray...) buf, err := cmd.Output() if err != nil { fmt.Fprintf(os.Stderr, "The command failed to perform: %s (Command: %s, Arguments: %s)", err, command, args) return } fmt.Fprintf(os.Stdout, "Result: %s", buf) } ```

谢哥们,解决了,参数问题。

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传