bash 如何在 Windows 中从 gitbash 运行 jq?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53967693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:04:37  来源:igfitidea点击:

How to run jq from gitbash in windows?

bashjqgit-bash

提问by DotNetDeveloper

I have gitbashin Windows. I am trying to run jqbut its giving me error.

gitbash在 Windows 中有。我正在尝试运行,jq但它给了我错误。

$ ./jq-win64.exe 
jq 
parse error: Invalid numeric literal at line 2, column 0

Intention: I want to use jqto parse json.

意图:我想jq用来解析json。

回答by VonC

Using jq-win64.exefrom github.com/stedolan/jq/releases, I get

使用jq-win64.exefrom github.com/stedolan/jq/releases,我得到

vonc@voncav MINGW64 /d/prgs/dl
$ ./jq-win64.exe --version
jq-1.6

vonc@voncav MINGW64 /d/prgs/dl
$ echo '{"foo": 0}' | ./jq-win64.exe .
{
  "foo": 0
}

So it does work, but it then depends on the json document you are parsing with it.
If that json document is not well-formed, that would generate the error you see.

所以它确实有效,但它取决于你正在解析的 json 文档。
如果该 json 文档格式不正确,则会生成您看到的错误。

In your bash session, you can define (or add to your ~/.bashrc) an alias:

在您的 bash 会话中,您可以定义(或添加到您的~/.bashrc)别名:

alias jq=/path/to/jq-win64.exe

That way, you don't need to use ./jq, but directly jq.

这样,您就不需要使用./jq,而是直接使用jq

$ echo '{"foo": 0}' | jq

In my case:

就我而言:

vonc@voncav:/$ alias jq=/mnt/d/dwnl/jq-win64.exe
vonc@voncav:/$ echo '{"foo": 0}' | jq
{
  "foo": 0
}

回答by bryanbcook

I hate answers that say you need another to use another tool to download, but using https://chocolatey.org/made this incredibly simple for me.

我讨厌那些说您需要另一个工具才能使用其他工具下载的答案,但使用https://chocolatey.org/使这对我来说非常简单。

From an elevated command-prompt, run:

从提升的命令提示符运行:

choco install jq

jqmagically works from bash going forward.

jq从 bash 开始神奇地工作。

回答by kidbrax

I just downloaded the binary to %HOMEPATH%/bin/jq-win64and it worked right away via jq

我刚刚下载了二进制文件%HOMEPATH%/bin/jq-win64,它立即通过jq