Golang中exec.Command中的字符转义

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

Golang character escape in exec.Command

问题

你好,我有一个用于exec.Command函数的命令行包装器,代码如下:

func GenCmd(cmdline string) *exec.Cmd {
    fmt.Println("command is ", cmdline)
    // 分割头部 => g++ 部分 => 剩余的命令
    parts := strings.Fields(cmdline)

    head := parts[0]
    parts = parts[1:len(parts)]

    // 执行命令并收集输出
    cmd := exec.Command(head, parts...)
    fmt.Printf("Generated comdline : %s", cmd)
    return cmd
}

func exeCmd(cmdline string, wg *sync.WaitGroup) {
    cmd := GenCmd(cmdline)
    out, err := cmd.Output()
    if err != nil {
        fmt.Printf("%s", err)
    }
    fmt.Printf("%s", out)
    wg.Done() // 通知等待组该goroutine已完成
}

我想在我的包装器中调用以下curl命令,原始curl命令如下:

curl -G https://api.github.com/search/repositories       \
    --data-urlencode "q=created:>`date -v-7d '+%Y-%m-%d'`" \
    --data-urlencode "sort=stars"                          \
    --data-urlencode "order=desc"                          \
    -H "Accept: application/vnd.github.preview"            \
    | jq ".items[0,1,2] | {name, description, language, watchers_count, html_url}"

但是似乎字符**- `**都需要在命令中进行转义:

`date -v-7d '+%Y-%m-%d'`

有什么办法可以做到这一点吗?

以下是主函数:

func main() {
    x := []string{
        `curl -G https://api.github.com/search/repositories ` +
            `--data-urlencode "q=created:>\`date -v-7d '+%Y-%m-%d'\`"` +
            `--data-urlencode 'sort=stars' --data-urlencode 'order=asc'` +
            `-H 'Accept: application/vnd.github.preview'` +
            `| jq ".items[0,1,2] | {name, description, language, watchers_count, html_url}"`,
    }

    cmdCnt := len(x)
    wg := new(sync.WaitGroup)
    wg.Add(cmdCnt)

    for _, cmd := range x {
        go exeCmd(cmd, wg) // 空字符串输出到stdout
    }

    wg.Wait()
}

这是完整的Go代码

英文:

Hello I have a command line wrapper for exec.Command function as below :

func GenCmd(cmdline string) *exec.Cmd {
	fmt.Println("command is ", cmdline)
	// splitting head => g++ parts => rest of the command
	parts := strings.Fields(cmdline)

	head := parts[0]
	parts = parts[1:len(parts)]

	// exec cmd & collect output
	cmd := exec.Command(head, parts...)
	fmt.Printf("Generated comdline : %s", cmd)
	return cmd
}

func exeCmd(cmdline string, wg *sync.WaitGroup) {
	cmd := GenCmd(cmdline)
	out, err := cmd.Output()
	if err != nil {
		fmt.Printf("%s", err)
	}
	fmt.Printf("%s", out)
	wg.Done() // signal to waitgroup this goroutine complete
}

I want to call a curl command as below in my wrapper , origin curl command as below:

curl -G https://api.github.com/search/repositories       \
    --data-urlencode "q=created:>`date -v-7d '+%Y-%m-%d'`" \
    --data-urlencode "sort=stars"                          \
    --data-urlencode "order=desc"                          \
    -H "Accept: application/vnd.github.preview"            \
    | jq ".items[0,1,2] | {name, description, language, watchers_count, html_url}"

But seems like character - ` both need to be escaped at command

`date -v-7d '+%Y-%m-%d'`

Any idead how can I do that ?

main function below:

func main() {
	x := []string{
		`curl -G https://api.github.com/search/repositories ` +
			`--data-urlencode "q=created:>\`date -v-7d '+%Y-%m-%d'\`" --data-urlencode 'sort=stars' --data-urlencode 'order=asc'` +
			`-H 'Accept: application/vnd.github.preview'` +
            `| jq ".items[0,1,2] | {name, description, language, watchers_count, html_url}"`
	}

	cmdCnt := len(x)
	wg := new(sync.WaitGroup)
	wg.Add(cmdCnt)

	for _, cmd := range x {
		go exeCmd(cmd, wg) // empty string output to stdout
	}

	wg.Wait()
}

Here is full golang code

答案1

得分: 2

你不能在反引号内部使用反引号,但你可以像这样构建长字符串:

`第一部分 
 ` + "第二部分`" + `...`
英文:

You cannot escape backticks inside backticks but you can build long string like this:

`first 
 part ` + "`second part`" + `…`

huangapple
  • 本文由 发表于 2022年6月18日 10:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/72666221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定