正确传递参数给Go Exec

huangapple go评论100阅读模式
英文:

Properly pass arguments to Go Exec

问题

我正在尝试学习Go语言,作为一个开始,我想尝试编写一个非常简单的用于控制我的iTunes的Web服务器。我过去经常使用osascript -e 'Tell Application "iTunes" to playpause'来实现这个目的,我以为我可以简单地将这个调用传递给osascript。

被注释掉的“say 5”命令确实起作用。

  1. package main
  2. import "exec"
  3. //import "os"
  4. func main() {
  5. var command = "Tell Application 'iTunes' to playpause"
  6. //var command = "say 5"
  7. c := exec.Command("/usr/bin/osascript", "-e", command)
  8. // c.Stdin = os.Stdin
  9. _, err := c.CombinedOutput()
  10. println(err.String());
  11. }

我收到的响应如下 -

  1. jessed@JesseDonat-MBP ~/Desktop/goproj » ./8.out
  2. exit status 1
  3. [55/1536]0x1087f000

我不太确定接下来该怎么做,任何指导将不胜感激。

英文:

I'm trying to learn go and as a start I wanted to try to throw together a super simple web server for controlling my iTunes. I've used osascript -e 'Tell Application "iTunes" to playpause' to this purpose many times in the past and thought I could simply sluff the call off to osascript here.

The commented out "say 5" command does work.

  1. package main
  2. import "exec"
  3. //import "os"
  4. func main() {
  5. var command = "Tell Application 'iTunes' to playpause"
  6. //var command = "say 5"
  7. c := exec.Command("/usr/bin/osascript", "-e", command)
  8. // c.Stdin = os.Stdin
  9. _, err := c.CombinedOutput()
  10. println(err.String());
  11. }

The response I am receiving from this is as follows -

  1. jessed@JesseDonat-MBP ~/Desktop/goproj » ./8.out
  2. exit status 1
  3. [55/1536]0x1087f000

I'm not exactly sure where to go from here and any direction would be greatly appreciated.

答案1

得分: 8

我用以下代码使其工作:

  1. package main
  2. import (
  3. "fmt"
  4. "exec"
  5. )
  6. func main() {
  7. command := "Tell Application \"iTunes\" to playpause"
  8. c := exec.Command("/usr/bin/osascript", "-e", command)
  9. if err := c.Run(); err != nil {
  10. fmt.Println(err.String())
  11. }
  12. }

我认为如果参数中有空格,exec.Command(...)会在其周围添加双引号,所以你只需要在需要的地方转义 "。

英文:

I got it working with this

  1. package main
  2. import (
  3. "fmt"
  4. "exec"
  5. )
  6. func main() {
  7. command := "Tell Application \"iTunes\" to playpause"
  8. c := exec.Command("/usr/bin/osascript", "-e", command)
  9. if err := c.Run(); err != nil {
  10. fmt.Println(err.String())
  11. }
  12. }

I think exec.Command(...) adds double quotes to the parameters if there is spaces in them, so you only need to escape " where you need them.

答案2

得分: 1

你可能只是缺少引号。试试这样写:

  1. var command = "\"Tell Application 'iTunes' to playpause\""

另外,Go语言的惯用写法通常是这样的:

  1. if err != nil {
  2. fmt.Println(err.String())
  3. }
英文:

Your are probably just missing quotes. Try:

  1. var command = "\"Tell Application 'iTunes' to playpause\""

Also, instead of println, idiomatic go usually looks like:

  1. if err != nil {
  2. fmt.Println(err.String());
  3. }

答案3

得分: 0

尝试

  1. c := exec.Command("/usr/bin/osascript", "-e", "say 5")
  2. output, err := c.CombinedOutput()

或者尝试

  1. c := exec.Command("/usr/bin/osascript", "-e", "say 5")
  2. c.Stdin = os.Stdin
  3. output, err := c.CombinedOutput()

打印错误(如果有)和合并的输出:

  1. if err != nil { fmt.Println(err) }
  2. fmt.Print(string(output))
英文:

Try

  1. c := exec.Command("/usr/bin/osascript", "-e", "say 5")
  2. output, err := c.CombinedOutput()

or try

  1. c := exec.Command("/usr/bin/osascript", "-e", "say 5")
  2. c.Stdin = os.Stdin
  3. output, err := c.CombinedOutput()

To print the error (if any) and the combined output:

  1. if err != nil { fmt.Println(err) }
  2. fmt.Print(string(output))

huangapple
  • 本文由 发表于 2011年10月21日 12:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/7845130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定