英文:
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()
}
英文:
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()
}
答案1
得分: 2
你不能在反引号内部使用反引号,但你可以像这样构建长字符串:
`第一部分
` + "第二部分`" + `...`
英文:
You cannot escape backticks inside backticks but you can build long string like this:
`first
part ` + "`second part`" + `…`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论