-1

As I am more of a web guy, i need a little help with this one, so I will explain what I am trying to do before I ask.

I am creating batch script that will:

  • GET request from an external server (json file), receive data, save locally as .json
  • Use JQ to navigate the json for result[0].title
  • Use the 'title' as a parameter for a curl request

Once I have the file locally. I would use JQ to find the data in the object

cat file.json | jq '.results[0].title' > $1 && curl -i -H "Accept: application/html" -H "Content-Type: application/html" -X GET http://example.com/test/$1 > test.txt

Is is possible to set local variables in command line '$1' for temporary use in a piped command?

Am I waaay off here?

1
  • 3
    v=$(jq ... file.json) && curl ... /$v ...
    – Jeff Schaller
    Commented Jan 9, 2016 at 1:52

1 Answer 1

3

While $1 typically has a special meaning (the first parameter passed to a script/function/etc.) you can indeed save the output of commands in variables.

title=$(jq -r '.results[0].title' file.json)
curl -i -H "Accept: application/html" -H "Content-Type: application/html" -X GET http://example.com/test/"$title" > test.txt

The first part runs the commands jq -r '.results[0].title' file.json and save the output (whatever shows up on stdout into the variable title. Then we run the curl command and expand the title variable as part of the URL.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.