首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在循环中运行curl会导致意外的输出。

问在循环中运行curl会导致意外的输出。
EN

Stack Overflow用户
提问于 2015-11-12 12:01:50
回答 1查看 1.2K关注 0票数 1

在curl中,我这样做是为了检查URL是否可用:

代码语言:javascript
复制
$ curl -u admin:secret -s http://test.app.com/doSomething -o /dev/null -w "%{http_code} %{url_effective}\\n"
200 http://test.app.com/doSomething

我有一个主机和路径列表,我想通过GET请求定期检查,输出应该显示响应状态和被调用的url。因此,对于一组主机tests.app.com、a1.app.com、p.app.com以及路径"/doSomething“和"doSomethingElse”,我将运行脚本并期望得到以下输出

代码语言:javascript
复制
200 http://test.app.com/doSomething
200 http://test.app.com/doSomethingElse
200 http://a1.app.com/doSomething
404 http://a1.app.com/doSomethingElse
200 http://p.app.com/doSomething
500 http://p.app.com/doSomethingElse

通过这种方式,我可以看到路径doSomethingElse不工作。

现在我的剧本是这样的:

代码语言:javascript
复制
#!/bin/bash

HOSTS=(
        'test.app.com'
        'a1.app.com'
        'p.app.com'
)
PATHS=(
        'doSomething'
        'doSomethingElse'
)

USER=admin
PASS=secret

# fixed parameters
auth_header="-u ${USER}:${PASS}"
out_parms="-w \"%{http_code} %{url_effective}\\\n\""

for h in "${HOSTS[@]}"; do
        for p in "${PATHS[@]}"; do
                curl ${auth_header} -s http://${h}/${p} -o /dev/null ${out_parms}
        done
done

它会产生几个curl调用(类似于顶部的示例),但是当我运行这个脚本时,输出如下所示

代码语言:javascript
复制
"200"000"200"000"200"000"200"000"200"000"200"000

注意:"200“似乎是状态代码,在我的设置中有6个请求。

如果我在curl命令(echo curl ${auth_header} ...)之前添加了一个echo curl ${auth_header} ...,我就会得到一个命令列表,这些命令可以从命令行执行,并导致预期的结果。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-23 22:10:31

获取bash到print out what it's executing for your curl command的

代码语言:javascript
复制
(set -x; curl ${auth_header} -s http://${h}/${p} -o /dev/null ${out_parms})

产生了下列输出:

代码语言:javascript
复制
++ curl -u admin:secret -s http://test.app.com/doSomething -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000++ curl -u admin:secret -s http://test.app.com/doSomethingElse -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000++ curl -u admin:secret -s http://a1.app.com/doSomething -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000++ curl -u admin:secret -s http://a1.app.com/doSomethingElse -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000++ curl -u admin:secret -s http://p.app.com/doSomething -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000++ curl -u admin:secret -s http://p.app.com/doSomethingElse -o /dev/null -w '"%{http_code}' '%{url_effective}\\n"'
"301"000

看着

代码语言:javascript
复制
-w '"%{http_code}' '%{url_effective}\\n"'

我们可以看到curl命令没有按照您希望的方式格式化

回答here时,应该总是将双引号放在变量替换周围;但是,没有理由将out_parms参数化。将这一定义插入如下:

代码语言:javascript
复制
curl ${auth_header} -s http://${h}/${p} -o /dev/null -w "%{http_code} %{url_effective}\n"

给了我你所期望的输出。

代码语言:javascript
复制
301 http://test.app.com/doSomething
301 http://test.app.com/doSomethingElse
301 http://a1.app.com/doSomething
301 http://a1.app.com/doSomethingElse
301 http://p.app.com/doSomething
301 http://p.app.com/doSomethingElse

或者,如果希望保留out_parms,则如果将out_parms更改为

代码语言:javascript
复制
out_parms="-w %{http_code} %{url_effective}\n"

并将双引号放在curl命令中。

代码语言:javascript
复制
curl ${auth_header} -s http://${h}/${p} -o /dev/null "${out_parms}"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33670959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档