jq内置方法select():找不到命令

时间:2018-09-27 13:11:39

标签: shell select jq

我使用jq内置方法select解析shell脚本中的json字符串,并收到错误:command not found

这是我的shell脚本:test.sh

#!/bin/bash

function test(){

   json='[{"id":1,"name":"jdjson"},{"id":2,"name":"imagetookit"}]'

   detail=`echo $json | jq .[]|select\(.id==2\)`
}

test

我只是在命令行上运行了脚本,并收到以下错误:

$ bash test.sh
test.sh:行5: select(.id==2): 未找到命令 (means "command not found") 

select是jq命令的内置方法,我不知道为什么

是否需要导入功能?如何导入jq的方法?

1 个答案:

答案 0 :(得分:3)

这不是一个WITH VTE AS ( SELECT * FROM (VALUES(1,0.5,NULL), (2,0.5,1), (3,0.5,NULL), (4,0.5,NULL), (5,0.5,0.8), (6,0.5,NULL)) V(WeekNo, Starting, New)), CTE AS( SELECT *, COUNT(New) OVER (ORDER BY WeekNo ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Grp FROM VTE) SELECT WeekNo, Starting, New, ISNULL(MAX(New) OVER (PARTITION BY CTE.Grp),Starting) AS Result FROM CTE ORDER BY WeekNo; 问题,而是一个shell引用问题:

jq

请注意mytest(){ json='[{"id":1,"name":"jdjson"},{"id":2,"name":"imagetookit"}]' detail=$(echo "$json" | jq '.[]|select(.id==2)') echo "$detail" } mytest { "id": 2, "name": "imagetookit" } 命令周围的单引号',因此不需要反斜杠jq
还希望使用\而不是旧的反引号表示法。