shell - 使用带有美元的 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
如何解决这个问题?
解决方案
&$
是外壳特殊字符。&
将前面的命令置于后台。$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
}
推荐阅读
- node.js - 猫鼬按对象中的值过滤嵌套的对象数组
- azure-active-directory - 在 Azure AD 中将 Okta 配置为 IDP 的详细步骤
- docker - crashloopbackoff 中的 Kubernetes pod
- react-apollo - 阿波罗客户端重新获取查询不使用反应挂钩传递变量
- java - 为什么本地声明的原语进入堆栈,而本地定义的对象进入堆?
- firebase - 如何使用 Google Apps 脚本将 GeoLocation 存储在 Firestore 中?
- javascript - 如何计算js中5或0分钟的任何时间剩余多少秒?
- python - Flask 引发 TemplateNotFound 错误,尽管在使用 EXPLAIN_TEMPLATE_LOADING 配置时识别出正确的目录
- powershell - Powershell 找不到 sysctl
- node.js - 使用 Elastic Beanstalk (AWS) 部署后删除的图像