Executing command with spaces in one of the parts

不打扰是莪最后的温柔 提交于 2020-11-29 10:34:50

问题


I am running a command through the os/exec package that is called like this:

out, err := Exec("ffprobe -i '/media/Name of File.mp3' -show_entries format=duration -v quiet -of csv=p=0", true, true)

The function I have written to execute command line calls is:

func Exec(command string, showOutput bool, returnOutput bool) (string, error) {
    log.Println("Running command: " + command)

    lastQuote := rune(0)
    f := func(c rune) bool {
        switch {
        case c == lastQuote:
            lastQuote = rune(0)
            return false
        case lastQuote != rune(0):
            return false
        case unicode.In(c, unicode.Quotation_Mark):
            lastQuote = c
            return false
        default:
            return unicode.IsSpace(c)
        }
    }

    parts := strings.FieldsFunc(command, f)
    //parts = ["ffprobe", "-i", "'/media/Name of File.mp3'", "-show_entries", "format=duration", "-v", "quiet", "-of", "csv=p=0"]
    if returnOutput {
        data, err := exec.Command(parts[0], parts[1:]...).Output()
        if err != nil {
            return "", err
        }
        return string(data), nil
    } else {
        cmd := exec.Command(parts[0], parts[1:]...)
        if showOutput {
            cmd.Stderr = os.Stderr
            cmd.Stdout = os.Stdout
        }
        err := cmd.Run()
        if err != nil {
            return "", err
        }
    }

    return "", nil
}

The strings.Fields command splits the command on spaces and that is used as the string array to pass to the exec.Command function. The problem is that it is splitting the filename into different parts because of the space when that filepath needs to stay together. Even if I format the string array correctly so the filepath is in one part, the exec.Command still fails because there is a space. I need to be able to execute this script to honor the filepath as one argument with spaces.


回答1:


  1. You may use strings.Split(s, ":") on special character like : and switch " with back-tick,
    Like this working sample (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], " "))
    }
}

output:

ffprobe
-i "/media/Name of File.mp3"
-show_entries format=duration
-v quiet
-of csv=p=0
  1. try print cmd.Args after cmd := exec.Command("ffprobe", s...) (remove .Output() ):

    for _, v := range cmd.Args { fmt.Println(v) }

something like this, to find out what happens to your args:

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)
}

see:

// 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
}

Edit 3- Try this

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())
}



回答2:


Well, I figured it out.

var parts []string
preParts := strings.FieldsFunc(command, f)
for i := range preParts {
    part := preParts[i]
    parts = append(parts, strings.Replace(part, "'", "", -1))
}

I needed to remove the single quotes from the arg passed into the exec.Command function.



来源:https://stackoverflow.com/questions/39151420/executing-command-with-spaces-in-one-of-the-parts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!