在Dollar中使用Azureure curl命令

时间:2019-03-22 23:31:42

标签: shell azure curl

嗨,我正在使用curl命令来获取天蓝色活动日志 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 该如何解决?

1 个答案:

答案 0 :(得分:0)

&$是shell特殊字符。 &将前面的命令放在后台。 $filter评估名为filter的变量,该变量显然为空。因此,您剩下的第二条命令=eventTimestamp带有一些参数,bash会抱怨。 通常用''引用您的网址。但这变得更加复杂,因为您在url中使用单引号。如果键入此内容,则需要在任何文字'之前关闭引号,并对每个\'使用',然后再次打开引号。

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"

使用网址编码特殊字符也可以。

如果需要,可以使用此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

}