简体   繁体   English

如何在 bash shell 的数组中转义星号“*”

[英]How to escape asterisk symbol '*' in an array in bash shell

I am a newbie for the bash shell.我是 bash shell 的新手。 The previous developer created the following codes to use a shell script to deploy AWS cloudformation stack.之前的开发人员创建了以下代码以使用 shell 脚本来部署 AWS cloudformation 堆栈。

  1. Read a json file and using jq to convert it to an array called parameters读取 json 文件并使用jq将其转换为称为parameters的数组
  2. Using ${parameters[@]} to spread into key=value pair strings使用${parameters[@]}传播到 key=value 对字符串

The problem is one array value is cron(0 20-6 * *? *) .问题是一个数组值是cron(0 20-6 * *? *) The deploy.sh will convert the * as current directory file names such as cron(0 20-6 CODEOWNERS README.md CODEOWNERS README.md? *) deploy.sh 会将*转换为当前目录文件名,如cron(0 20-6 CODEOWNERS README.md CODEOWNERS README.md? *)

My question: How can I let my deploy.sh to escape the * characters when calling ${parameters[@]} .我的问题:如何让 deploy.sh 在调用${parameters[@]}时转义*字符。

Here are the example codes:以下是示例代码:

dev.json开发者json

{
  "Parameters": {
    "Environment": "dev",
    "CrawlerScheduleExpression": "cron(0 20-6  * * ? *)"
  },
  "Tags": {
    "Severity": "High",
    "Environment": "dev",
    "DeletionPolicy": "Retain"
  }
}

deploy.sh部署.sh

parameters=($(jq -r '.Parameters | keys[] as $k | "\($k)=\(.[$k])"' "dev.json")) 
tags=($(jq -r '.Tags | keys[] as $k | "\($k)=\(.[$k])"' "${conf_file}"))

    aws cloudformation deploy \
    --s3-bucket ${BUILD_ARTIFACTS_BUCKET} \
    --s3-prefix ${PROJECT_NAME} \
    --template-file ${template} --stack-name ${STACK_NAME} \
    --parameter-overrides "${parameters[@]}" \ # Environment=dev CrawlerScheduleExpression=cron(0 20-6 CODEOWNERS README.md CODEOWNERS README.md ? *) 
    --tags "${tags[@]}"  

In the end, I have to disable the bash wildcard expansion to achieve my goal:(最后,我必须禁用 bash 通配符扩展才能实现我的目标:(

set noglob on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM