Not able to pass argument to curl command in shell script - bash

I am trying to run this shell script but the value of val1 is not being replaced in the CURL command
gcs_ip="172.16.0.81:8080"
run(){
val1=$1
echo "Migration with clickhouse worker thread: $1, batchsize: , consumer thread: , pagesize:" | tee -a log_migr.log result_migr.log
echo "curl -X PUT "http://$gcs_ip/gcs/api/v1/config""
curl -X PUT "http://$gcs_ip/gcs/api/v1/config" -H "accept: application/json" -H "Content-Type: application/json" -d '[ { "application": "pm", "instance": "-", "partition": "clickhouse", "name": "migration.clickhouse.workers", "value": "$val1" }]'
}
run 10

Related

Iterate through the json object and run curl command as variable from json output using jq

I would like to filter the json object while iterating through it and run curl command over each item from the output.
JSON object.
{
"repo": "releases",
"path": "/apps/releases",
"created": "2021-04-01T10:12:23.496-01:00",
"children": [
{
"uri": "/Image1",
"folder": true,
"created": 2022-08-09T17.12.22.987.04.000
},
{
"uri": "/Image2",
"folder": true,
"created": 2022-06-10T10.12.22.412.10.000
},
{
"uri": "/Image3",
"folder": true,
"created": 2022-10-10T07.03.14.742.01.000
},
{
"uri": "/Image4",
"folder": true,
"created": 2022-10-10T07.010.11.542.08.000
}
]
}
Looking for some logic that will iterate through the uri under children and that is passed through curl command as $i which would be Image1, Image2 and Image3.
curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/${i}"
While I was running this below command and the output is as follows
for i in $(curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/" | jq -c ".children[] |.uri)
Output: ["/Image1", "/Image2", "/Image3"]
I tried the following command but in the output it replaces ${i} with only Image3, somehow it is not taking Image1 and Image2.
for i in $(curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/" | jq -r ".children[] |.uri); do curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/${i}"; done
I tried the following command but in the output it replaces ${i} with only Image3, somehow it is not taking Image1 and Image2.
curl can read URLs to fetch from a file, which you can generate with jq. Something like
base=https://artifactory.com/api/releases/baseimage
curl -k -s --user user:password -X GET "$base/" |
jq -r --arg b "$base" '.children[].uri | "url = \"\($b)/\(.)\""' |
curl -k -s --user user:password -X GET --config -
Just one curl process to fetch all the individual images, and no shell loop needed.
You might find it easier to construct the for loop along the following lines:
for uri in $( echo "$json" | jq '.children[].uri') ; do
echo curl ... ${uri}...
done

How to add def variable into echo commnad inside a shell script in jenkins?

script {
now = new Date()
time = now.format("%D", TimeZone.getTimeZone('IST'))
CREATE = sh (
script: ' echo "curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H \\"Content-Type: application/json\\" -d \' { \\"description\\": \\" Descrption of ticket\\", \\"subject\\": \\"Ticket from Jenkins ${time}\\", \\"email\\": \\" tom#outerspace.com \\", \\"priority\\": 1, \\"status\\": 2}\' -X POST \'https://srikartest1.freshpo.com/api/v2/tickets\' | python -m json.tool"',
returnStdout: true
)
echo "${CREATE}"
}
in the output the time is not inserting the echo command
output:
echo 'curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H "Content-Type: application/json" -d '\'' { "description": " Descrption of ticket", "subject": "Ticket from Jenkins ", "email": " tom#outerspace.com ", "priority": 1, "status": 2}'\'' -X POST '\''https://srikartest1.freshpo.com/api/v2/tickets'\'' | python -m json.tool'
you have to escape the $ in groovy -
script {
now = new Date()
time = now.format("dd", TimeZone.getTimeZone('IST'))
CREATE = $/ sh (
script: ' echo "curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H "Content-Type: application/json" -d ' { "description": " Descrption of ticket", "subject": "Ticket from Jenkins ${time} ", "email": " tom#outerspace.com ", "priority": 1, "status": 2}' -X POST 'https://srikartest1.freshpo.com/api/v2/tickets' | python -m json.tool"',
returnStdout: true
) /$
echo "${CREATE}"
}

Parse curl response to variable and use it in curl

I have the following script:
#!/bin/bash
TOKEN=$(curl -isX POST 'http://localhost:3005/auth/tokens' \
-H 'Content-Type: application/json' \
-d '{
"name": "test#test.de",
"password": "1234"
}' | grep X-Subject-Token | sed "s/X-Subject-Token: //g")
echo $TOKEN
curl --trace test.txt -X POST "http://localhost:3005/v1/users" \
-H "Content-Type: application/json" \
-H "X-Auth-token: $TOKEN" \
-d '{
"user": {
"username": "alice",
"email": "alice#test.com",
"password": "test"
}
}'
The command echo $TOKEN is printing the right result (something like 35be3d05-7f80-4b11-ad20-7a7110e9d3a7). From the last curl request I get the following error from curl:
curl: (52) Empty reply from server
If I write above the last curl request TOKEN="35be3d05-7f80-4b11-ad20-7a7110e9d3a7" the request is working. So I guess there is something wrong with the TOKEN variable.
Kindly Regards
EDIT:
Output from declare -p TOKEN :
"eclare -- TOKEN="6770806a-1230-4f64-b519-1841e9deb5f1
I had to remove an carriage return. Thanks to #chepner!
Solution:
TOKEN=$(curl -isX POST 'http://localhost:3005/auth/tokens' \
-H 'Content-Type: application/json' \
-d '{
"name": "test#test.de",
"password": "1234"
}' | grep X-Subject-Token | sed "s/X-Subject-Token: //g" | tr -d '\r')

jq shell script: aggregate iteration content into json body

I'm creating a shell script.
I need to create a curl json body according the a key-value list content. This content is splitted by way of a awk which generate a two column table:
KVS_VARIABLES=$(awk -F= '!($1 && $2 && NF==2) { print "File failed validation on line " NR | "cat 1>&2"; next } { print $1, $2 }' $f)
Example output:
VAR1 VAL1
VAR2 VAL2
VAR3 VAL3
So, this table is iterated on a while iteration and each key and value are splitted:
echo "$KVS_VARIABLES" | while read -r kv
do
key=$(echo $kv | awk '{print $1}')
value=$(echo $kv | awk '{print $2}')
done
So, I need some way to aggregate this content into a json document in order to send it out using curl:
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<-EOF
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
"$key": "$value" <<<<<<<<<<<<<(1)>>>>>>>>>>>>>>
}
}
EOF
So, on <<<<<<<<<<<<<(1)>>>>>>>>>>>>>> I need to aggregate each key and value propagation.`
So, in this case I'd need to generate:
"VAR1": "VAL1",
"VAR2": "VAL2",
"VAR3": "VAL3"
and then insert it inside "stringData":
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
<<<<<<<<<<<<<(1)>>>>>>>>>>>>>>
}
}
So, after all:
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
"VAR1": "VAL1",
"VAR2": "VAL2",
"VAR3": "VAL3"
}
}
jq is installed.
Any ideas?
You don't need an awk statement inside the while loop, but just read the key value pairs inside the read command itself.
Also storing awk output in a variable and later parsing is an anti-pattern. You could use the process substitution feature provided by the shell, the < <() part will slurp the output of a command as if it were appearing on a file (or) use the here-strings
json=$(cat <<-EOF
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
}
}
EOF
)
while read -r key value; do
json=$(echo "$json" | jq ".stringData += { \"$key\" : \"$value\" }")
done< <(awk -F= '!($1 && $2 && NF==2) { print "File failed validation on line " NR | "cat 1>&2"; next } { print $1, $2 }' $f)
You could now use the variable "$json" in the curl as
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<<"$json"

Curl Multiline on Command Windows 10

How i can send multiline via curl on Windows 10 ?
This is my CURL command :
curl -X POST \
-H "Accept: application/json" \
-H "X-Access-Token: ZmQ780ODRmNDlkMjMzYjA0OTA1Y54545jY5ODQ3MjM0YmNjYjM5ODA1OWU5NTE1YjEwNTdjMjcxMzQ3ZDdjMzMxNDA0ZA==" \
-H "Cache-Control: no-cache" \
-d '{
"frames": [
{
"text": "USDCHF",
"icon": "i34",
"index": 0
}
]
}' \
https://developer.myurl.com/api/v1/dev/widget/update/com.myurl.7a9c72294c097a37f0f0a3337f5ad444598f21/1
PS: Line by line work perfectly
Thanks

Resources