Running curl in a loop results in unexpected output

时间:2015-11-12 12:01:50

标签: bash curl

In curl, I do something like this to check if a URL is available:

$ 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

I have a list of hosts and paths I want to check regularily using a GET request and the output should show me the response status and the url that was called. So, for a set of hosts tests.app.com, a1.app.com, p.app.com and the paths "/doSomething" and "doSomethingElse" I would run the script and expect the following output

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

This way I can see that the path doSomethingElse does not work.

Now my script looks like this:

#!/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

It produces several curl calls (similar to the example at the top), but when I run this script, the output looks like the following

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

Note: The "200" seems to be the status code and there are 6 requests in my setup.

If I add an echo before the curl command (echo curl ${auth_header} ...), I get a list of commands that I can execute from the command line and which lead to the expected result.

What am I doing wrong?

1 个答案:

答案 0 :(得分:3)

使用

将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}"