【问题标题】:combining multiple outputs in one line in a function在函数的一行中组合多个输出
【发布时间】:2019-12-31 10:17:47
【问题描述】:

我正在尝试构建一个函数或脚本,逐行从文件中读取 url 并测试相应 curl 调用的状态代码。

一般来说通话效果很好,但我在两件事上失败了:

  • 文本文件中的最后一个 URL 被忽略。
  • 我无法在一行中输出带有 url 的状态代码,最好是制表符分隔

infile p.e.看起来像这样:

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

第一个回显只输出包含空格的 $line(来自文件的 url) - 而不是状态码 + url 第二个 echo 输出状态码没问题 第三个 echo 输出 url ($line) 好。

如何将它们组合成一行?

我尝试将回声值放在括号中(圆形,卷曲......) 我尝试用 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
}

我正在寻找的输出类似于

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

【问题讨论】:

    标签: bash scripting output


    【解决方案1】:

    HTTP 协议使用\r\n 分隔行,但head 不知道这一点,它保留\r

    用途:

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

    删除\r

    或者,您可以使用 dos2unix 更改行尾:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2017-10-28
      • 1970-01-01
      相关资源
      最近更新 更多