Problem mit dem Ersetzen der Json-Variablen durch den Shell-Skriptwert mit curl

473
user967468
#!/bin/bash #CONFIG_FILE_PATH is the path of the json file as argument while running the script CONFIG_FILE_PATH=$1 CUST_NAME=$2 curl -X POST -i -H "Accept: application/json" -H "Content-Type:application/json" --data-binary @$CONFIG_FILE_PATH "http://localhost:8080/service" 

Unten ist der Json, bei dem ich versuche, $ durch die Shell-Skriptvariable CUST_NAME zu ersetzen. Aber nicht funktionieren. Kann jemand dazu helfen?

{ "queries": [ { "query": "select * from customer where account_name like '$'" } ] } 
0

1 Antwort auf die Frage

0
Eugen Rieck

Die Art und Weise, wie Sie sie CONFIG_FILE_PATHin Ihrer curlZeile verwenden, wird von der Shell nicht gelesen und analysiert, sodass keine Variablenersetzung erfolgt. Es gibt viele Möglichkeiten, dies zu umgehen, aber ich bevorzuge meine eigene Ersetzung über sed:

JSON-Vorlage:

{ "queries": [ { "query": "select * from customer where account_name like '##CUST_NAME##'" } ] } 

Skript:

#!/bin/bash #CONFIG_FILE_PATH is the path of the json file as argument while running the script CONFIG_FILE_PATH=$1 CONFIG_FILE=$(cat "$CONFIG_FILE_PATH" | sed "s/##CUST_NAME##/$2/g") curl -X POST -i -H "Accept: application/json" -H "Content-Type:application/json" --data-binary "$CONFIG_FILE" "http://localhost:8080/service"