Taieb
Taieb

Reputation: 920

Get curl response and parameter in the same line

I have a file containing values:

file.txt

value1
value2
value3

And I have this script:

#!/bin/bash
input="test.txt"
while IFS= read -r line ; do 
    curl -o /dev/null -s -w "%{http_code}\n" -u username:token "https://website.com/api/user"$line
done < "$input" > result.txt

At the moment this is my output:

result.txt

400
200
200

What I really want to achieve is to get this kind of output:

output.txt

value1,400
value2,200
value3,200

What my code is missing to achieve this?

Upvotes: 1

Views: 1342

Answers (1)

costaparas
costaparas

Reputation: 5237

Run and capture the output of the curl command using the command substitution syntax $(...).

Then, use echo or printf to show the captured output, along with the original input, in the form you want.

input="test.txt"
while IFS= read -r line ; do 
    res=$(curl -o /dev/null -s -w "%{http_code}\n" -u username:token "https://website.com/api/user$line")
    printf "%s\n" "$line,$res"
done < "$input" > result.txt

Upvotes: 2

Related Questions