在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 curl ${auth_header} ...,我就会得到一个命令列表,这些命令可以从命令行执行,并导致预期的结果。
我做错了什么?
发布于 2016-11-23 22:10:31
获取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}"https://stackoverflow.com/questions/33670959
复制相似问题