1

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:

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

Here is my bash script:

#!/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?

Secespitus
  • 710
  • 2
  • 14
  • 22
0x00b0
  • 343
  • 1
  • 3
  • 17
  • Try to manually curl 1 URL? – Thanh Nguyen Van Jun 27 '19 at 09:21
  • Can you post an example of the content of `${FILE_NAME}`? – danrodlor Jun 27 '19 at 09:22
  • 1
    use double quotes for your variables : `save_log "$HTTP_STATUS" "$HTTP_RESPONSE"` – Aserre Jun 27 '19 at 09:42
  • 1
    Also, you are using a loop, but in your `save_log` function you use `> output.log`. `>` will overwrite the document each time. Use `>> output.log` to append text to a file. – Aserre Jun 27 '19 at 09:46
  • Possible duplicate of [How to append output to the end of a text file](https://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-a-text-file) – Aserre Jun 27 '19 at 09:48
  • Regarding your quotation problem : https://stackoverflow.com/questions/17094086/passing-arguments-with-spaces-between-bash-script – Aserre Jun 27 '19 at 09:50

1 Answers1

2

From the Curl documentation: -I, --head Show document info only Removing the -I or replace it with -i should solve your problem