首页 > 解决方案 > 使用带有美元的 azure curl 命令

问题描述

您好我正在使用 curl 命令获取 azure 活动日志 curl -X GET https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxx/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2019-03-18T20:00:00Z' and eventTimestamp le '2019-03-23T20:00:00Z' and resourceGroupName eq 'xxxx'

它给出错误 bash: =eventTimestamp: command not found 如何解决这个问题?

标签: shellazurecurl

解决方案


&$是外壳特殊字符。&将前面的命令置于后台。$filter评估名为 filter 的变量,该变量显然是空的。所以你留下了=eventTimestamp带有一些参数的第二个命令和 bash 抱怨。一般在''. 但它变得更加复杂,因为您在 url 中有单引号。如果你输入这个,你需要在任何文字之前关闭引用'并使用\'for each'然后再次打开引用。

curl -X GET 'https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxx/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '\''2019-03-18T20:00:00Z'\'' and eventTimestamp le '\''2019-03-23T20:00:00Z'\'' and resourceGroupName eq '\''xxxx'\''

您可以通过在文件中键入这样的 url 并将其内容读入 shell 变量来绕过这个问题,如下所示:

url=$(cat url_file)

或以此处文档的形式:

url=$(cat <<'EOF'
https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxx/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2019-03-18T20:00:00Z' and eventTimestamp le '2019-03-23T20:00:00Z' and resourceGroupName eq 'xxxx'
EOF
)

然后做:

curl -X GET "$url"

编码特殊字符的 URL 也可以。

如果你需要它,你可以使用这个 shell 函数来引用包含单引号的字符串:

quotifySingle() {
    #does the same as
    #printf %s\\n "$(printf %s "$1" | sed "s/'/'\\\\''/g")"
    local input="$1"
    local tmp=''

    while [ ${#input} -gt 0 ]
    do
      tmp=${input#?}
      case ${input} in
        \'*)   printf \'\\\'\'      ;;
          *)   printf %s "${input%"${tmp}"}"  ;;
      esac
      input=${tmp}
    done

}

推荐阅读