简体   繁体   English

bash 单引号扩展问题

[英]bash problem with single quotes expansion

I got the following script:我得到了以下脚本:

#!/usr/bin/env bash

# break on error
set -ex

## USAGE: ./open <type> <instance-name>
if [[ $# -ne 2 ]]; then
    echo "## USAGE: ${0} <type> <instance-name>"
    echo "type: team or project"
    echo "instance-name: name of the instance to fetch the URL from"
    exit 1
fi

ORGANIZATION="redacted_org"

PROJECT="redacted_project"

TYPE="redacted_type"
if [[ $1 == "team" ]]; then
  TYPE="redacted_type_team"
fi

NAME=$2

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "\"[? contains(path, '\\\\stacks\\\\${TYPE}\\\\instances\\\\${NAME}') && name=='apply'].{id:id}\"" \
  --output tsv)

echo "https://redacted_org.visualstudio.com/redacted_project/_build?definitionId=${BUILD_ID}"

The problem is that the BUILD_ID variable is being executed as (printed by the set -x ):问题是 BUILD_ID 变量被执行为(由set -x打印):

++ az pipelines build definition list --organization redacted_org --project redacted_project --query '"[? contains(path, \'\''\\stacks\\redacted_type\\instances\\redacted_2\'\'') && name==\'\''apply\'\''].{id:id}"' --output tsv

Note the '" instead of " just after the --query parameter and in the end of it, and the \'\' everywhere I have a single quote instead of '请注意'"而不是"就在 --query 参数之后和它的末尾,并且在我有一个单引号而不是 ' 的任何地方都有\'\' '

I need this BUILD_ID to be executed as follows, this command works fine when typed in the terminal itself:我需要按如下方式执行此 BUILD_ID,在终端本身中键入此命令时可以正常工作:

$(az pipelines build definition list --organization "redacted_org" --project "redacted_project" --query "[? contains(path, '\\stacks\\redacted_type\\instances\\redacted_2') && name=='apply'].{id:id}" --output tsv)

What am I doing wrong?我究竟做错了什么?

In the stanza:在节中:

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "\"[? contains(path, \'\\\\stacks\\\\${TYPE}\\\\instances\\\\${NAME}\') && name==\'apply\'].{id:id}\"" \
  --output tsv)

you've used some bizarre quoting.你使用了一些奇怪的引用。 You want that to be just the way you expect:您希望这正是您期望的方式:

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "[? contains(path, '\\stacks\\${TYPE}\\instances\\${NAME}) && name=='apply'].{id:id}" \
  --output tsv)

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

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