我有一个有效的shell脚本,该脚本读取包含URL的文本文件,每个URL都在单独的一行上。会从文件中并行读取URL,并检查其状态代码,并将响应写入到status-codes.csv中。
如何将从url-list.txt引用的原始URL写入status-codes.csv中输出的第一列?
status-codes.sh
#!/bin/bash
xargs -n1 -P 10 curl -u user:pass -L -o /dev/null --silent --head --write-out '%{url_effective},%{http_code},%{num_redirects}\n' < url-list.txt | tee status-codes.csv
url-list.txt
http://website-url.com/path-to-page-1
http://website-url.com/path-to-page-2
http://website-url.com/path-to-page-3
status-codes.csv (当前输出)
http://website-url.com/path-to-page-2,200,1
http://website-url.com/path-to-page-after-any-redirects,200,2
http://website-url.com/404,404,2
status-codes.csv (所需的输出)
http://website-url.com/path-to-page-2,http://website-url.com/path-to-page-2,200,1
http://website-url.com/path-to-page-1,http://website-url.com/path-to-page-after-any-redirects,200,2
http://website-url.com/path-to-page-3,http://website-url.com/404,404,2
答案 0 :(得分:2)
使用-I
选项。例如:
xargs -n1 -P 10 -I '{}' curl -u user:pass -L -o /dev/null --silent --head --write-out '{},%{url_effective},%{http_code},%{num_redirects}\n' '{}' < url-list.txt | tee status-codes.csv
man xargs
:
-I replace-str 用从标准输入中读取的名称替换出现在初始参数中的 replace-str 。