Golang 'exec.Command' 对待单引号参数特殊?
Golang 'exec.Command' treat single quote parameter special?
我参考 post How to execute system command with unknown arguments? 到 运行 我的 ubuntu shell 上的一个 jq 命令。
下面是我试过的代码
import (
"fmt"
"os/exec"
"sync"
"strings"
)
func exeCmd(cmd string, wg *sync.WaitGroup) {
fmt.Println("command is ",cmd)
// splitting head => g++ parts => rest of the command
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head,parts...).Output()
if err != nil {
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
wg.Done() // Need to signal to waitgroup that this goroutine is done
}
func main() {
wg := new(sync.WaitGroup)
wg.Add(1)
x := []string{"jq '(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}' long-response.json > short-response.json"}
exeCmd(x[0], wg)
wg.Wait()
}
输出如下,似乎命令被正确检测到,但是 shell returns 退出状态 3 是“没有这样的过程”?
command is jq '(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}' long-response.json > short-repsonse.json exit status 3
有人可以帮忙吗?
我想要的是一个可以包装器和 运行 bash 命令行的 go 函数,就像我在 Linux shell 上做的一样
PS:当我将上面尝试的 jq 命令粘贴到我的 Linux shell
时,它运行良好
尝试了其他方法:删除了我的 jq 命令中的单引号 并且我的命令以我期望的输出执行 - 一个已解析的 json 文件
但是,我仍然有一个存在状态 2,任何人都可以解释
- 为什么我的命令行中的单引号会影响 G 解析命令的方式?
- 为什么我的 shell 命令完成后我仍然存在 2?
程序执行 jq 命令,而不是 shell。 jq 命令不理解传递给命令的 shell 语法(引号和 I/O 重定向)。
使用以下代码 运行 stdout 的命令重定向到 short-response.json.
cmd := exec.Command("jq",
"(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}",
"long-response.json")
f, err := os.Create("short-response.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
cmd.Stdout = f // set stdout to short-response.json
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
我参考 post How to execute system command with unknown arguments? 到 运行 我的 ubuntu shell 上的一个 jq 命令。 下面是我试过的代码
import (
"fmt"
"os/exec"
"sync"
"strings"
)
func exeCmd(cmd string, wg *sync.WaitGroup) {
fmt.Println("command is ",cmd)
// splitting head => g++ parts => rest of the command
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head,parts...).Output()
if err != nil {
fmt.Printf("%s", err)
}
fmt.Printf("%s", out)
wg.Done() // Need to signal to waitgroup that this goroutine is done
}
func main() {
wg := new(sync.WaitGroup)
wg.Add(1)
x := []string{"jq '(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}' long-response.json > short-response.json"}
exeCmd(x[0], wg)
wg.Wait()
}
输出如下,似乎命令被正确检测到,但是 shell returns 退出状态 3 是“没有这样的过程”?
command is jq '(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}' long-response.json > short-repsonse.json exit status 3
有人可以帮忙吗? 我想要的是一个可以包装器和 运行 bash 命令行的 go 函数,就像我在 Linux shell 上做的一样 PS:当我将上面尝试的 jq 命令粘贴到我的 Linux shell
时,它运行良好尝试了其他方法:删除了我的 jq 命令中的单引号 并且我的命令以我期望的输出执行 - 一个已解析的 json 文件 但是,我仍然有一个存在状态 2,任何人都可以解释
- 为什么我的命令行中的单引号会影响 G 解析命令的方式?
- 为什么我的 shell 命令完成后我仍然存在 2?
程序执行 jq 命令,而不是 shell。 jq 命令不理解传递给命令的 shell 语法(引号和 I/O 重定向)。
使用以下代码 运行 stdout 的命令重定向到 short-response.json.
cmd := exec.Command("jq",
"(.data.legacyCollection.collectionsPage.stream.edges|map({node:(.node|{url,firstPublished,headline:{default:.headline.default},summary})})) as $edges|{data:{legacyCollection:{collectionsPage:{stream:{$edges}}}}}",
"long-response.json")
f, err := os.Create("short-response.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
cmd.Stdout = f // set stdout to short-response.json
err = cmd.Run()
if err != nil {
log.Fatal(err)
}