linux - 在 bash 脚本中循环执行命令时,如何将来自 cURL 的响应主体保存在文件中?

标签 linux bash shell ubuntu-16.04

我创建了一个 cURL bash 脚本,我想在其中将响应主体保存到名为 output.log 的文件中,但是当我打开文件 output.log 时,它看起来像这样:

screenshot of the output.log file showing a line for the Header Code, a line for the execution date and HTTP/1.1 for the Response Body

这是我的 bash 脚本:

#!/bin/bash
SECRET_KEY='helloWorld'
FILE_NAME='sma.txt'

function save_log()
{

    printf '%s\n' \
    "Header Code    : $1" \
    "Executed at    : $(date)" \
    "Response Body  : $2" \
    '==========================================================\n'  > output.log
}

while IFS= read -r line; 
    do 
        HTTP_RESPONSE=$(curl -I -L -s -w "HTTPSTATUS:%{http_code}\\n" -H "X-Gitlab-Event: Push Hook" -H 'X-Gitlab-Token: '$SECRET_KEY --insecure $line 2>&1)
        HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

        save_log $HTTP_STATUS $HTTP_RESPONSE
done < $FILE_NAME

谁能帮我在我的 output.log 中得到我想要的输出?

最佳答案

来自 Curl 文档: -I, --head 只显示文档信息 删除 -I 或将其替换为 -i 应该可以解决您的问题

关于linux - 在 bash 脚本中循环执行命令时,如何将来自 cURL 的响应主体保存在文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787716/

相关文章:

linux - Bash hashmap 使用引号作为键

linux - 如何更改多个文件的第一行(包括文件名)?

bash - 读取日志文件以提取各个字段并统计发生次数

linux - 当我在 Linux Fedora 中运行 JavaFx 应用程序时,我的应用程序崩溃了..!

linux - Sudo 将中断导入我的脚本

linux - 是否可以编写一个在 bash/shell 和 PowerShell 中运行的脚本?

linux - 如何在 linux 中打印字符串中的特定字符?

shell - 可恢复脚本的最佳选择

bash - 将 4 个文件合并为 1 个具有不同列的文件

c - 这个文件操作线程安全吗?