2

I have a text file containing variables of fqdn and hostname. File looks like this

first_fqnd    first_hostname
second_fqdn     second_hostname
.....      .....

I have to update some data using curl in a bash script, but I have to take fqdn and hostname from this text file and make a curl for every pair fqdn and hostname.

My curl should be like this:

curl -H "Content-Type:application/json" -XPUT "https://pup.pnet.pl/api/hosts/**fqdn from file**" -d '{"host":{"name": "**hostname from file**"}}' --cacert bundle.pem --cert xxx-pem.cer --key xxx-privkey.pem

How can I pass these variables from file to curl? I've thought about using awk, but I don't know how to use it in curl command

2 Answers 2

5

Use a while construct to read the lines of file and put whitespace separated parameters as two relevant variables, fqdn and hostn:

while read fqdn hostn; do
    curl -H .... -XPUT "https://pup.pnet.pl/api/hosts/${fqdn}" \
      -d '{"host":{"name": "'"${hostn}"'"}}' --cacert ....; done <file.txt
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need backslash after do
2

Try something like this:

#!/bin/bash

while read fqdn hostname; do
    curl -H "Content-Type:application/json" -XPUT \
        "https://pup.pnet.pl/api/hosts/${fqdn}" \
        -d '{"host":{"name": "'${hostname}'}}' --cacert bundle.pem \
        --cert xxx-pem.cer --key xxx-privkey.pem
done <input_file.txt

The while read fqdn hostname will take input from Standard Input, line by line, splitting it by Bash's Internal Field Separator into "column" variables $fqdn and $hostname. See Catching User Input for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.