将自定义变量传递到包含curl命令选项的bash文件中

将自定义变量传递到包含curl命令选项的bash文件中

curl在 bash 文件中给出一个工作命令(带选项):

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

像这样,使用shell这里-文档

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

相关内容