Bash 执行官Cmd快把我逼疯了

Bash 执行官Cmd快把我逼疯了,bash,go,command-line,command-line-arguments,Bash,Go,Command Line,Command Line Arguments,故事是这样的:我有一个名为coletorbeat的程序,它需要一个大参数才能运行。此参数基于几个参数。我构建了第二个名为bootstrapper的程序,它接受这些参数,构建coletorbeat所需的长参数,然后运行coletorbeat,并将长参数提供给它。 我尝试了多种方法,包括转义引号、反勾号等等。但由于某种原因,程序被调用,就好像根本没有传递任何参数一样。我甚至没有错误信息。 我将在下面发布代码,非常感谢您的帮助 像coletorbeat.ip=%v这样的字段应该出现在命令行中的引号或反

故事是这样的:我有一个名为coletorbeat的程序,它需要一个大参数才能运行。此参数基于几个参数。我构建了第二个名为bootstrapper的程序,它接受这些参数,构建coletorbeat所需的长参数,然后运行coletorbeat,并将长参数提供给它。 我尝试了多种方法,包括转义引号、反勾号等等。但由于某种原因,程序被调用,就好像根本没有传递任何参数一样。我甚至没有错误信息。 我将在下面发布代码,非常感谢您的帮助

像coletorbeat.ip=%v这样的字段应该出现在命令行中的引号或反勾号内,前面是标志-E

package main

import (
    "fmt"
    "os"
    "os/exec"
    "time"
)

func main() {
    if len(os.Args) < 8 {
        fmt.Printf("%v arguments were given. 8 needed to work", len(os.Args))
        return
    }
    // Parâmetros devem ser passados na ordem: ip, porta, data_inicio, data_fim, tipo_equipamento, versao, nivel, instituicao
    //There can`t be empty spaces on any of the strings inside the Args. We gotta do some prevention in case it happens here.

    flag := "-E"
    Arg1 := fmt.Sprintf("`coletorbeat.ip=%v`", os.Args[1])
    Arg2 := fmt.Sprintf("`coletorbeat.porta=%v`", os.Args[2])
    Arg3 := fmt.Sprintf("`coletorbeat.dataInicio=%v`", os.Args[3])
    Arg4 := fmt.Sprintf("`coletorbeat.dataFim=%v`", os.Args[4])
    Arg5 := fmt.Sprintf("`coletorbeat.tipoEquipamento=%v`", os.Args[5])
    Arg6 := fmt.Sprintf("`coletorbeat.versao=%v`", os.Args[6])
    Arg7 := fmt.Sprintf("`coletorbeat.nivel=%v`", os.Args[7])
    Arg8 := fmt.Sprintf("`coletorbeat.instituicao=%v`", os.Args[8])
    Arg9 := fmt.Sprintf("`output.elasticsearch.index=collectorbeat-%v-%v-%v-%v`", os.Args[8], os.Args[5], time.Now().Format("20060201"), os.Args[1])

    commandToExecute := &exec.Cmd{
        Path: "coletorbeat",
        Args: []string{"./", flag, Arg1, flag, Arg2, flag, Arg3, flag, Arg4, flag, Arg5, flag, Arg6, flag, Arg7, flag, Arg8, flag, Arg9},       
        Stdout: os.Stdout,
        Stderr: os.Stdout,
    }
    fmt.Println(commandToExecute.Args)
    fmt.Println("Copy the code")
    if err := commandToExecute.Run(); err != nil {
        fmt.Println("error caught on Bootstrapper")
        fmt.Println("Error: ", err)
    }
}


奇怪的是,以下解决方案解决了这个问题:

func main() {
    if len(os.Args) < 8 {
        fmt.Printf("%v arguments were given. 8 needed to work", len(os.Args))
        return
    }

    cmd := exec.Command("full/canonical/executable/file/path", "-E", fmt.Sprintf("\"coletorbeat.ip=%v\"", os.Args[1]), "-E", fmt.Sprintf("\"coletorbeat.porta=%v\"", os.Args[2]), "-E", fmt.Sprintf("\"coletorbeat.dataInicio=%v\"", os.Args[3]), "-E", fmt.Sprintf("\"coletorbeat.dataFim=%v\"", os.Args[4]), "-E", fmt.Sprintf("\"coletorbeat.tipoEquipamento=%v\"", os.Args[5]), "-E", fmt.Sprintf("\"coletorbeat.versao=%v\"", os.Args[6]), "-E", fmt.Sprintf("\"coletorbeat.nivel=%v\"", os.Args[7]), "-E", fmt.Sprintf("\"coletorbeat.instituicao=%v\"", os.Args[8]), "-E", fmt.Sprintf("\"output.elasticsearch.index=collectorbeat-%v-%v-%v-%v\"", os.Args[8], os.Args[5], time.Now().Format("20060201"), os.Args[1]))

    fmt.Println(cmd.Args)

    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err := cmd.Run()
    fmt.Println(fmt.Sprint(err) + ": " + out.String())
}


即使参数包含空格,也不要使用引号。您正在准备通过shell调用args。在直接执行程序时,您不需要使用引号。可以尝试以下几点:1使用单引号而不是反勾号。2制作一个包含所有参数的长字符串,就像在bash命令行中键入它一样,并将该字符串作为单个参数传递。3制作一个新程序,只读取args并将其注销,将其替换为coletorbeat,看看您是否得到了预期的args。4将-E作为arg的一部分,而不是它们自己的,例如-E'coletorbeat.ip=%v',其实很简单:因为没有涉及shell,所以传递给exec.Command的参数,或者在本例中分配给Cmd.Args的参数,在调用的程序中成为os.Args,而没有任何更改。因此,除非coletorbeat需要非常不寻常的iteral引号,否则请将它们全部删除。此外,由于您是手动构建Cmd,因此必须使用程序名coletorbeat作为Args的第一个元素:Args:[]string{coletorbeat,./…}。我强烈建议您改用exec.Command。