【问题标题】:Running curl in a loop results in unexpected output在循环中运行 curl 会导致意外输出
【发布时间】:2015-11-12 12:01:50
【问题描述】:

在 curl 中,我执行以下操作来检查 URL 是否可用:

$ 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”,我将运行脚本并期待以下输出

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这个路径不起作用。

现在我的脚本如下所示:

#!/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 调用(类似于顶部的示例),但是当我运行这个脚本时,输出如下所示

"200"000"200"000"200"000"200"000"200"000"200"000

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

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

我做错了什么?

【问题讨论】:

  • 大括号扩展(即 "{a,b}/{1,2}" -> "a/1 a/2 b/1 b/2")可能会帮助您简化代码。
  • 您是否可以将您的问题完全改写为:“如果我运行 curl ... 我会得到“200 http://...”。如果我将相同的命令放在循环,输出变为“....”。
  • curl参数-o-w可以混用吗?
  • @MichaWiedenmann 我重写了这个问题。感谢您的建议。更改-o-w 参数的位置不会更改输出
  • 我仍然想知道,是否可以在同一命令中使用-o-w,在我看来它们是冲突的。

标签: bash curl


【解决方案1】:

使用 bash 到 print out what it's executing for your curl command

(set -x; curl ${auth_header} -s http://${h}/${p} -o /dev/null ${out_parms})

生成以下输出:

++ 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

看着

-w '"%{http_code}' '%{url_effective}\\n"'

我们可以看到 curl 命令的格式不符合您的要求

正如here 的回答,您应该始终在变量替换周围加上双引号;但是,没有理由参数化 out_parms。像这样将该定义内联:

curl ${auth_header} -s http://${h}/${p} -o /dev/null -w "%{http_code} %{url_effective}\n"

以您期望的格式给我这个输出。

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 更改为,则会生成相同的输出

out_parms="-w %{http_code} %{url_effective}\n"

在 curl 命令中加上双引号

curl ${auth_header} -s http://${h}/${p} -o /dev/null "${out_parms}"

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多