【问题标题】:JQ query in powershell is not working with variable in SELECT [duplicate]Powershell中的JQ查询不适用于SELECT中的变量[重复]
【发布时间】:2021-10-09 18:48:35
【问题描述】:

在 Power shell 中,我尝试运行 ORacle Cloud CLI 命令并使用 JQ 对其进行过滤。 如果我在 JQ select 中使用硬编码值,它工作正常..但如果我使用变量,我不会得到任何数据。请帮忙。

在选择中使用硬编码值:

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

选择中带有变量的失败代码:

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

【问题讨论】:

  • 简而言之:只有 "..." 字符串(双引号)在 PowerShell 中执行字符串插值(变量值的扩展),而不是 '...' 字符串(单引号):请参阅 this answer PowerShell 的可扩展字符串(插值字符串)概述和this answer 概述 PowerShell 字符串文字的一般概述。

标签: powershell oracle-cloud-infrastructure


【解决方案1】:

PowerShell 不会扩展单引号字符串中的变量。

$ociResult = oci compute instance list-vnics --all --compartment-id xxxxx --profile myprof
$jqExpr = '.data[]|select(.\"hostname-label\"==\"' + $host_name + '\")'

$ociResult | C:\myfiles\jq-win64.exe $jqExpr

【讨论】:

  • 谢谢,成功了。
猜你喜欢
  • 2022-01-23
  • 2021-09-18
  • 2016-02-25
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多