亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在其中一個部分執行帶有空格的命令

在其中一個部分執行帶有空格的命令

Go
慕神8447489 2022-03-03 16:04:12
我正在通過調用如下的os/exec包運行命令:out, err := Exec("ffprobe -i '/media/Name of File.mp3' -show_entries format=duration -v quiet -of csv=p=0", true, true)我編寫的用于執行命令行調用的函數是: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}該strings.Fields命令將命令拆分為空格,并將其用作字符串數組以傳遞給 exec.Command 函數。問題在于它將文件名分成不同的部分,因為filepath需要保持在一起的空間。即使我正確格式化了字符串數組,所以filepath它在一個部分中,exec.Command仍然會失敗,因為有一個空格。我需要能夠執行此腳本以將filepath空格作為一個參數。
查看完整描述

2 回答

?
慕桂英3389331

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之后嘗試打?。篶md := 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())

}


查看完整回答
反對 回復 2022-03-03
?
慕妹3146593

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 中刪除單引號。


查看完整回答
反對 回復 2022-03-03
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號