如何在Shell管道中使用`jq`? [英] How to use `jq` in a shell pipeline?
问题描述
我似乎无法让jq
在Shell管道中表现为正常".例如:
I can't seem to get jq
to behave "normally" in a shell pipeline. For example:
$ curl -s https://api.github.com/users/octocat/repos | jq | cat
仅打印其帮助文本即可得到jq
.如果我尝试将jq
的输出重定向到文件,则会发生相同的事情:
results in jq
simply printing out its help text*. The same thing happens if I try to redirect jq
's output to a file:
$ curl -s https://api.github.com/users/octocat/repos | jq > /tmp/stuff.json
jq
是否确定是否不是从tty运行它而故意救助吗?如何防止这种行为,以便可以在管道中使用jq
?
Is jq
deliberately bailing out if it determines that it's not being run from a tty? How can I prevent this behavior so that I can use jq
in a pipeline?
* (我意识到此示例包含对猫的无用使用; (仅用于说明目的)
* (I realize this example contains a useless use of cat; it's for illustration purposes only)
推荐答案
您需要提供一个过滤器作为参数.要通过未经修饰的jq
提供的默认值以外的其他方式传递JSON,请使用标识过滤器.
:
You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing jq
provides by default, use the identity filter .
:
curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
这篇关于如何在Shell管道中使用`jq`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!