1

Given a working curl command (with options) in a bash file:

#!/bin/bash

curl 'https://digi.kansalliskirjasto.fi/rest/binding-search/search/binding?offset=0&count=10000' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-H 'Pragma: no-cache' \
--data-raw '{   "authors":[],
                "collections":[],
                "districts":[],
                "endDate":null,
                "formats":["JOURNAL","PRINTING","NEWSPAPER"],
                "fuzzy":false,
                "hasIllustrations":false,
                "importStartDate":null,
                "importTime":"ANY",
                "includeUnauthorizedResults":false,
                "languages":[],
                "orderBy":"RELEVANCE",
                "pages":"",
                "publicationPlaces":[],
                "publications":[],
                "publishers":[],
                "query":"freedom", 
                "queryTargetsMetadata":false,
                "queryTargetsOcrText":true,
                "requireAllKeywords":true,
                "searchForBindings":false,
                "showLastPage":false,
                "startDate":null,
                "tags":[]
            }' \
--compressed \
--output my_file.json

I would like to pass the following custom arguments:

myQUERY="freedom"
myFORMATS='["JOURNAL","PRINTING","NEWSPAPER"]'
myFUZZY="false"

to my --data-raw option as variables for "query", "formats" and "fuzzy". I tried several alternatives:

"formats":$myFORMATS, "query":$myQUERY, "fuzzy":$myFUZZY,

or

"formats":${myFORMATS}, "query":${myQUERY}, "fuzzy":${myFUZZY}

or

"formats":"${myFORMATS}", "query":"${myQUERY}", "fuzzy":"${myFUZZY}"

neigther of those returned desired resutls of the initial bash code!

What is the easiest way to manipulate the custom variable into a command option?

Cheers,

1 Answer 1

1

Like this, using shell here-doc:

myquery="freedom"
myformats='["JOURNAL","PRINTING","NEWSPAPER"]'
myfuzzy="false"

curl 'https://digi.kansalliskirjasto.fi/rest/binding-search/search/binding?offset=0&count=10000' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-H 'Pragma: no-cache' \
--compressed \
--output my_file.json \
-d @- <<EOF
{   "authors":[],
    "collections":[],
    "districts":[],
    "endDate":null,
    "formats":$myformats,
    "fuzzy":$myfuzzy,
    "hasIllustrations":false,
    "importStartDate":null,
    "importTime":"ANY",
    "includeUnauthorizedResults":false,
    "languages":[],
    "orderBy":"RELEVANCE",
    "pages":"",
    "publicationPlaces":[],
    "publications":[],
    "publishers":[],
    "query":"$myquery", 
    "queryTargetsMetadata":false,
    "queryTargetsOcrText":true,
    "requireAllKeywords":true,
    "searchForBindings":false,
    "showLastPage":false,
    "startDate":null,
    "tags":[]
} 
EOF
1
  • Thanks, it works! I just used notations $myFORMATS, $myQUERY and $myFUZZY in my question.
    – doplano
    Nov 17, 2022 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .