简体   繁体   English

如何使用bash脚本在文件中输出第一行curl结果

[英]How to output first line of curl result in a file with bash script

I want to read a file of urls, curl each url and only get the first line which contains the HTTP code. 我想读取一个URL文件,卷曲每个URL,仅获取包含HTTP代码的第一行。 I am running under Windows 10 inside Cmder. 我正在Cmder的Windows 10下运行。

#!/bin/bash
input="urls.csv"
truncate -s 0 dest.csv
while IFS= read -r var
do
    result= `curl -I ${var%$'\r'} | grep HTTP $result`
echo "$var $result" >> dest.csv
done < "$input"

however the output file is empty, thank you 但是输出文件为空,谢谢

Assuming urls.csv is just a simple list of URLs and you're working on a linux system (or any system which has /dev/null ), following command will send HEAD requests to each URL and output them next to HTTP response codes. 假设urls.csv只是URL的简单列表,并且您正在Linux系统(或任何具有/dev/null )上工作,则以下命令将HEAD请求发送到每个URL并将它们输出到HTTP响应代码旁边。

sed 's/^/url = /; s/\r\?$/\n-o \/dev\/null/' urls.csv |
curl -s -K- -w '%{http_code} %{url_effective}\n' -I >outfile

see curl man page for further information. 有关更多信息,请参见curl手册页

In reply to your query on outputting the HTTP code, this is given in the second line of the header. 为了回答您有关输出HTTP代码的查询,这在标题的第二行中给出。 You may get this by: 您可以通过以下方式获得此信息:

export IFS=$; # This is required to stop everything coming out on one line!
curl -i <domain> | head -n 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM