英文:
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)) | ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论