简体   繁体   English

在bash脚本中循环执行命令时,如何从cURL将响应正文保存到文件中?

[英]How to save the response body from cURL in a file when executing the command in a loop in a bash script?

I've created a cURL bash script in which I want to save the response body into a file called output.log , but when I open the file output.log it looks like this: 我创建了一个cURL bash脚本,其中要将响应正文保存到名为output.log的文件 ,但是当我打开文件output.log时,它看起来像这样:

output.log文件的屏幕快照,显示标题代码行,执行日期行和响应正文的HTTP / 1.1

Here is my bash script: 这是我的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

Can anyone help me get my desired output in my output.log? 谁能帮助我在output.log中获得所需的输出?

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

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

相关问题 cURL Bash将响应正文保存到文件 - cURL Bash save response body to file curl命令不是通过bash中的shell脚本执行的 - curl command not executing via shell script in bash 从脚本运行时,Bash脚本无法正确执行命令 - Bash script not executing command properly when run from script 如何从可执行bash脚本中的curl获得正确的响应? - How to get correct response from curl in executable bash script? 如何使用 SendGrid 的 SMTP API 从带有 curl 命令的 bash 脚本发送一个简单的 HTML 文件? - How to send a simple HTML file from a bash script with the curl command using SendGrid's SMTP API? 如何使用curl命令从bash脚本获取HTTP响应? - How I get the HTTP responses from bash script with curl command? 如何在参数中使用curl命令并从Python脚本执行命令(Python 2.6.6) - How to use curl command with parameters and executing the command from Python script(Python 2.6.6)) 从Windows bat文件执行bash脚本 - Executing a bash script from a windows bat file 从Cron作业执行bash脚本时出现“ / bin / bash ^ M:错误的解释器:没有这样的文件或目录”错误 - “/bin/bash^M: bad interpreter: No such file or directory” error when executing a bash script from a cron job 如何在Linux bash shell脚本的curl命令中使用变量来发送带有文件的发布请求? - How to use variable in curl command in Linux bash shell script to send post request with file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM