简体   繁体   English

在外壳程序脚本中使用jq

[英]using jq within a shell script

Suspect it's a newbie mistake that I'm making, but in an attempt to parse a json such as 怀疑这是我正在犯的新手错误,但试图解析json,例如

{"current_observation": {"observation_time": "Last Updated on July 4, 12:53 PM CDT" } }

from within a script such as 从诸如

#!/bin/sh

jq '.' test.json

I get an error response from ./test.sh as follows 我从./test.sh收到错误响应,如下所示

syntax error, unexpected INVALID_CHARACTER, expecting $end '.'1 compile error

Now if I do a jq '.' 现在,如果我做一个jq'。 test.json from the command line in terminal, it performs as expected, but when attempting to use it within a script it fails as detailed. 从终端中的命令行运行test.json,它按预期执行,但是尝试在脚本中使用它时,失败的原因如详述。

Anyone able to point out my error? 有人能指出我的错误吗?

I can reproduce your error with this script: 我可以使用以下脚本重现您的错误:

#!/bin/sh   
jq \'.\' test.json

I think you just need to get rid of the quotes around the . 我认为您只需要删除。中的引号即可. Although it works fine for me with unescaped quotes. 尽管对我来说,使用转义的引号可以很好地工作。

Try it just like this: 像这样尝试:

#!/bin/sh   
jq . test.json

It might be related to this bug report for jq. 可能与此jq的错误报告有关。 (although probably not as it works for you on the command line) (尽管可能不行,因为它在命令行上对您有用)

Comma produces the error. 逗号产生错误。 You should quote it: 您应该引用它:

{"current_observation": {"observation_time": "Last Updated on July 4\u002c 12:53 PM CDT" } }

Test: 测试:

$ jq '.' temp.txt 
{
  "current_observation": {
    "observation_time": "Last Updated on July 4, 12:53 PM CDT"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM