【问题标题】:Jq - Syntax error when ran on windows powershell however on bash terminal it was successful [duplicate]Jq - 在 Windows Powershell 上运行时出现语法错误,但在 bash 终端上运行成功 [重复]
【发布时间】:2021-10-10 04:06:31
【问题描述】:

我试图使用 JQ 解析 OCI JSON 输出并在 Windows powershell 中出现语法错误,但是相同的命令在 Bash 终端中运行良好。 如果我遗漏了什么,请告诉我。

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxxx  --profile myprof |C:\myfiles\jq-win64.exe ".data[]|select(."hostname-label"=="test1" )"

jq: error: syntax error, unexpected $end, expecting ';' or ')' (Windows cmd shell quoting issues?) at <top-level>, line 1: .data[]|select(. jq: 1 compile error PS C:myfiles>

尝试使用单引号中的 JQ 语句,它给出以下语法错误:

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(."hostname-label"=="test1" )'

jq: error: syntax error, unexpected ==, expecting '$' (Windows cmd shell quoting issues?) at <top-level>, line 1: .data[]|select(.hostname-label==test1 ) jq: 1 compile error PS C:\MYDATA\myfiles>

我在 jplay.org 中尝试过同样的方法,它在那里也运行良好。请告诉我如何在 Windows 中解决此问题。

谢谢

【问题讨论】:

  • PowerShell 7.1 的可悲现实是,在传递给 的参数中需要一个 额外的手动\ -转义嵌入的 " 字符外部程序。这可能在 7.2 中得到修复,可能需要选择加入。有关详细信息,请参阅链接副本的 this answer

标签: powershell jq oracle-cloud-infrastructure


【解决方案1】:

单引号内的双引号需要转义:

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(.\"hostname-label\"==\"test1\")'

如果您有变量,请将其作为参数传递:

PS C:\myfiles> oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe --arg host_name $host_name '.data[]|select(.\"hostname-label\"==$host_name)'

【讨论】:

  • 如何在我的选择条件中有变量的情况下获得正确的输出? select(.\"hostname-label\"==\"$host_nme\").id conidtion 失败,我正在获取所有数据而不是我要过滤的主机。
  • 尝试了以下方法:select(.\"hostname-label\"==\"$host_nme\") 没有失败,但没有显示数据。
猜你喜欢
  • 2020-09-19
  • 2019-07-18
  • 2020-12-06
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多