简体   繁体   English

在功能中将多条输出组合在一行中

[英]combining multiple outputs in one line in a function

im trying to build a function or script that reads urls from a file line by line and tests for the status code of the corresponding curl call. 我试图建立一个函数或脚本来逐行从文件中读取URL,并测试相应curl调用的状态代码。

the call in general works fine, but i fail in 2 things: 一般而言,该呼叫工作正常,但我在两件事上失败了:

  • the last URL from the textfile gets ignored. 文本文件中的最后一个URL将被忽略。
  • i won't manage to ouput the status code with the url in one line, ideally tab-separated 我不会用网址在一行中输出状态代码,最好用制表符分隔

the infile pe looks like this: infile pe如下所示:

https://facebook.com
https://google.com
https://stackoverflow.com

the first echo only outputs the $line (url from file) including a blank space - instead of the status code + the url the second echo outputs the status code allright the third echo outputs the url ($line) allright. 第一个回显仅输出$ line(文件中的URL),包括空格-而不是状态代码+ url,第二个回显输出状态代码正常,第三个回显输出url($ line)正常。

How do I combine them in one line only? 如何仅将它们组合成一行?

I tried putting the echo values in brackets (round, curly...) I tried replacing line breaks with the tr command and sed 我尝试将回声值放在方括号(圆形,卷曲...)中,尝试用tr命令替换换行符并使用sed

testredirects() {

if test -f "$1"; then

while IFS= read -r line; do
     output=$(curl -Is $line | head -1 ) 
     echo $output $line
     echo $output
     echo $line
done < "$1"

fi
}

the output i am looking for is something like 我正在寻找的输出是这样的

HTTP/2 301 Moved permanently ; https://google.com

The HTTP protocol uses \\r\\n to separate lines, but head doesn't know that and it keeps the \\r . HTTP协议使用\\r\\n分隔行,但是head不知道,并且保留\\r

Use: 采用:

output=$(curl -Is https://google.com | head -1 | tr -d "\r")

to remove the \\r 删除\\r

Alternatively, you can use dos2unix to change the line endings: 另外,您可以使用dos2unix更改行尾:

output=$(curl -Is https://google.com | dos2unix | head -1)

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

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