[英]bash problem with single quotes expansion
我得到了以下腳本:
#!/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}"
問題是 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
請注意'"
而不是"
就在 --query 參數之后和它的末尾,並且在我有一個單引號而不是 ' 的任何地方都有\'\'
'
我需要按如下方式執行此 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)
我究竟做錯了什么?
在節中:
BUILD_ID=$(az pipelines build definition list \
--organization "${ORGANIZATION}" \
--project "${PROJECT}" \
--query "\"[? contains(path, \'\\\\stacks\\\\${TYPE}\\\\instances\\\\${NAME}\') && name==\'apply\'].{id:id}\"" \
--output tsv)
你使用了一些奇怪的引用。 您希望這正是您期望的方式:
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.