2 回答
TA贡献2036条经验 获得超8个赞
您可以strings.Split(s, ":")在特殊字符上使用并使用反引号:进行切换 ,例如 这个工作示例(The Go Playground):"
package main
import (
"fmt"
"strings"
)
func main() {
command := `ffprobe : -i "/media/Name of File.mp3" : -show_entries format=duration : -v quiet : -of csv=p=0`
parts := strings.Split(command, ":")
for i := 0; i < len(parts); i++ {
fmt.Println(strings.Trim(parts[i], " "))
}
}
输出:
ffprobe
-i "/media/Name of File.mp3"
-show_entries format=duration
-v quiet
-of csv=p=0
在(删除)cmd.Args之后尝试打印:cmd := exec.Command("ffprobe", s...).Output()
对于 _, v := 范围 cmd.Args { fmt.Println(v) }
像这样,找出你的参数发生了什么:
s := []string{"-i '/media/Name of File.mp3'", "-show_entries format=duration", "-v quiet", "-of csv=p=0"}
cmd := exec.Command("ffprobe", s...)
for _, v := range cmd.Args {
fmt.Println(v)
}
cmd.Args = []string{"ffprobe", "-i '/media/Name of File.mp3'", "-show_entries format=duration", "-v quiet", "-of csv=p=0"}
fmt.Println()
for _, v := range cmd.Args {
fmt.Println(v)
}
看:
// Command returns the Cmd struct to execute the named program with
// the given arguments.
//
// It sets only the Path and Args in the returned structure.
//
// If name contains no path separators, Command uses LookPath to
// resolve the path to a complete name if possible. Otherwise it uses
// name directly.
//
// The returned Cmd's Args field is constructed from the command name
// followed by the elements of arg, so arg should not include the
// command name itself. For example, Command("echo", "hello")
func Command(name string, arg ...string) *Cmd {
cmd := &Cmd{
Path: name,
Args: append([]string{name}, arg...),
}
if filepath.Base(name) == name {
if lp, err := LookPath(name); err != nil {
cmd.lookPathErr = err
} else {
cmd.Path = lp
}
}
return cmd
}
编辑3-试试这个
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command(`ffprobe`, `-i "/media/Name of File.mp3"`, `-show_entries format=duration`, `-v quiet`, `-of csv=p=0`)
for _, v := range cmd.Args {
fmt.Println(v)
}
fmt.Println(cmd.Run())
}
TA贡献1820条经验 获得超9个赞
嗯,我想通了。
var parts []string
preParts := strings.FieldsFunc(command, f)
for i := range preParts {
part := preParts[i]
parts = append(parts, strings.Replace(part, "'", "", -1))
}
我需要从传递给 exec.Command 函数的 arg 中删除单引号。
- 2 回答
- 0 关注
- 179 浏览
添加回答
举报