正确传递参数给Go Exec

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

Properly pass arguments to Go Exec

问题

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

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

package main

import "exec"
//import "os"

func main() {

	var command = "Tell Application 'iTunes' to playpause"
	//var command = "say 5"

	c := exec.Command("/usr/bin/osascript", "-e", command)
//	c.Stdin = os.Stdin
	_, err := c.CombinedOutput()
	println(err.String());


}

我收到的响应如下 -

jessed@JesseDonat-MBP ~/Desktop/goproj » ./8.out
exit status 1
[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.

package main

import "exec"
//import "os"

func main() {

	var command = "Tell Application 'iTunes' to playpause"
	//var command = "say 5"

	c := exec.Command("/usr/bin/osascript", "-e", command)
//	c.Stdin = os.Stdin
	_, err := c.CombinedOutput()
	println(err.String());


}

The response I am receiving from this is as follows -

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

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

答案1

得分: 8

我用以下代码使其工作:

package main

import (
    "fmt"
    "exec"
)

func main() {
    command := "Tell Application \"iTunes\" to playpause"
    
    c := exec.Command("/usr/bin/osascript", "-e", command)
    if err := c.Run(); err != nil {
        fmt.Println(err.String())
    }
}

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

英文:

I got it working with this

package main

import (
	"fmt"
	"exec"
)

func main() {
	command := "Tell Application \"iTunes\" to playpause"
	
	c := exec.Command("/usr/bin/osascript", "-e", command)
	if err := c.Run(); err != nil {
		fmt.Println(err.String())
	}
}

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

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

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

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

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

Your are probably just missing quotes. Try:

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

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

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

答案3

得分: 0

尝试

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

或者尝试

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

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

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

Try

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

or try

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

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

if err != nil { fmt.Println(err) }
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:

确定