curlコマンドオプションを含むbashファイルにカスタム変数を渡す

curlコマンドオプションを含むbashファイルにカスタム変数を渡す

curlBashファイルから作業コマンド(オプションを含む)を提供します。

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

次のカスタムパラメータを渡したいです。

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

、およびの変数--data-rawとしてオプションがあります。私はいくつかの選択肢を試しました。"query""formats""fuzzy"

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

または

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

または

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

これらのどれも初期のbashコードで要求された結果を返しません!

カスタム変数をコマンドオプションで操作する最も簡単な方法は何ですか?

乾杯、

ベストアンサー1

このようにシェルを使ってここ - ドキュメント:

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

おすすめ記事