Bash脚本使用jq: –arg值在脚本中未被使用。

huangapple go评论57阅读模式
英文:

Bash script using jq: --arg value not utilized with script

问题

Sure, here's the translated content of the code you provided:

我有一个Bash脚本,在其中传递了一个参数,我试图在随后的jq查询中使用它,但我无法让它起作用。当在CLI中使用手动输入时,该命令可以工作,但在将参数传递给脚本时则不行。对于我哪里出错的任何建议将不胜感激!

#!/bin/bash
ACCOUNT=123456789
ENTITY=$1
DASHBOARDS=$(aws quicksight list-dashboards --aws-account-id ${ACCOUNT} | jq -r --arg e "$ENTITY" '.DashboardSummaryList[] | select(.Name|contains("$e")) | .DashboardId')

另一个问题是,select contains 区分大小写。为了解决这个问题,我首先确保参数被转换为全小写,以便在if语句中捕获它。该语句处理全大写的特殊情况(如"UK"),或返回一个默认值,其中第一个字符大写,其余字符小写。

ENTITY=$(echo "$1" | tr '[A-Z]' '[a-z]')  #将参数设置为全小写

if [ "$ENTITY" = "uk" ] || [ "$ENTITY" = "exception2" ]; then
  ENTITY=$(echo ${ENTITY^^}) #全大写
else
  ENTITY=$(echo ${ENTITY^}) #第一个字符大写
fi
英文:

I have a bash script where a parameter is passed in which I am trying to use in a subsequent jq query, but I cannot get it to work. The command works when using manual inputs in cli, but not when passing the parameter to a script. Any suggestions on where I'm going wrong would be greatly appreciated!

#!/bin/bash
ACCOUNT=123456789
ENTITY=$1
DASHBOARDS=$(aws quicksight list-dashboards --aws-account-id ${ACCOUNT} | jq -r --arg e "$ENTITY" '.DashboardSummaryList[] | select(.Name|contains("$e")) | .DashboardId')

Another problem I ran into is that select contains is case sensitive. To solve it I first made sure the parameter is translated into full lowercase to be caught in a if statement. This statement handles full uppercase exceptions (such as "UK"), or returns a default value of first character being uppercase and the rest lowercase

ENTITY=$(echo "$1" | tr '[A-Z]' '[a-z]')  #Set parameter all lowercase

if [ "$ENTITY" = "uk" ] || [ "$ENTITY" = "exception2" ]; then
  ENTITY=$(echo ${ENTITY^^}) #All uppercase
else
  ENTITY=$(echo ${ENTITY^}) #First char uppercase
fi

答案1

得分: 1

你正在检查.Name是否包含字面字符串$e;变量不需要用引号括起来。

英文:

You are checking if .Name contains the literal string $e; the variable does not need to be quoted.

... select(.Name|contains($e)) | ...

huangapple
  • 本文由 发表于 2023年3月15日 20:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定