Put variable in bash for loop - bash

I am making a post request in bash multiple times with a for loop like:
for n in {1..5}; do curl -X POST 'http://localhost:8080/hello' -H 'accept: */*' -H 'Content-Type: application/json' -d '{
"a": 1,
"b": 2
}';done
Now the values a and b are the same so I wanted to generate random values for the variables.So I created two random values :
a= awk -v min=10 -v max=20 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'
b= awk -v min=50 -v max=60 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'
and tried to insert them into my post request like this:
for n in {1..5}; do curl -X POST 'http://localhost:8080/hello' -H 'accept: */*' -H 'Content-Type: application/json' -d '{
"a": a,
"b": b
}';done
but it didnt work I also tried :
for (n in {1..5};a= awk -v min=10 -v max=20 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'; b= awk -v min=50 -v max=60 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'); do curl -X POST 'http://localhost:8080/hello' -H 'accept: */*' -H 'Content-Type: application/json' -d '{
"a": 1,
"b": 2
}';done
but that also didnt work, what am I doing wrong?
Thanks in regard

You must perform some small corrections so you can use all of this in a script.
Next you can see the values being replaced (a and b). Adapt for what you need
a=`awk -v min=10 -v max=20 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'`
b=`awk -v min=50 -v max=60 -v num=1 'BEGIN{srand(); for (i=1;i<=num;i++) printf ("%.5f\n",min+rand()*(max-min+1))}'`
for n in {1..5}; do
echo curl -X POST 'http://localhost:8080/hello' -H 'accept: */*' -H 'Content-Type: application/json' -d "{
\"a\": $a,
\"b\": $b
}";done
The first problem is that you want a and b to have the result of awk command. For that you need to (for example) enclose the command between ``
To have the variables being substituted you cannot have them between '...' (single quotes). Otherwise they are not substituted. So you must use double quotes "..." to have $a and $b being substituted by their variable values.

Related

cURL using bash script with while read text file

Hi there anyone there having the same trouble like mine?
whenever I cURL the $list from the list.txt it just displaying {} which is a blank response from the API does my code should be really working properly or it is just a bug?
I know the $list is working because I can update the database status
Please this is a bit urgennnnttt :(
#! /bin/bash
filename=/var/lib/postgresql/Script/list.txt
database='dbname'
refLink='URL'
authorization='Authorization: Basic zxc'
expireDate=$(date -d "+3 days")
body="Message."
while IFS=' ' read -r list
do
wow=$(curl --location --request POST $refLink \
--header 'Authorization: Basic $authorization' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"Expiration Notice",
"body":"$body",
"min" :[{"mobileNumber" : "$list"}],
"type" : "Notification",
"action_type" : "NotificationActivity"}')
echo "result: '$result'"
RESP=$(echo "$result" | grep -oP "^[^a-zA-Z0-9]")
echo "RESP:'$RESP'"
echo $body
#echo $wow >> logs.txt
psql -d $database -c "UPDATE tblname SET status='hehe' WHERE mobile='$list'"
done < $filename
Your "$list" JSON entry is not populated with the content of the $list variable because it is within single quotes of the --data-raw curl parameter.
What you need is compose your JSON data for the query before-hand, preferably with the help of jq or some other JSON processor, before sending it as argument to the curl's POST request.
Multiple faults in your scripts (not exhaustive):
Shebang is wrong with a space #! /bin/bash
expireDate=$(date -d "+3 days") return date in locale format and this may not be what you need for your request.
The request and the response data are not processed with JSON grammar aware tools. grep is not appropriate for JSON data.
Some clues but cannot fix your script more without knowing more about the API answers and functions you use.
Anyway here is how you can at least compose a proper JSON request:
#!/usr/bin/env bash
filename='/var/lib/postgresql/Script/list.txt'
database='dbname'
refLink='URL'
authorization='zxc'
expireDate=$(date -R -d "+3 days")
body="Message."
while IFS=' ' read -r list; do
raw_json="$(
jq -n --arg bdy "$body" --arg mobN "$list" \
'.action_type="NotificationActivity"|.title="Expiration Notice"|.type="Notification"|.body=$bdy|.min[0].mobileNumber=$mobN|.'
)"
json_reply="$(curl --location --request POST "$refLink" \
--header "Authorization: Basic $authorization" \
--header 'Content-Type: application/json' \
--data-raw "$raw_json")"
echo "json_reply: '$json_reply'"
echo "$body"
# psql -d "$database" -c "UPDATE tblname SET status='hehe' WHERE mobile='$list'"
done <"$filename"

How to pass arguments to a curl command in a bash method [duplicate]

This question already has answers here:
Using curl POST with variables defined in bash script functions
(12 answers)
Closed 4 years ago.
I have a method where I'm executing a curl command and it returns the result. I would like to pass arguments to this method but it isn't working.
commande_mine() {
local MY_OUTPUT=$(curl -X POST \
http://localhost:5000/myapp \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"filepath": "$1"
}')
echo $MY_OUTPUT
}
for f in "/Users/anthony/my files"/*
do
commande_mine $f >> test.txt
break # break first time until everything works as expected
done
How can I use the $f parameter passed into the function inside the curl command?
You can use:
commande_mine() {
local MY_OUTPUT=$(curl -X POST \
http://localhost:5000/myapp \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"filepath": "'"$1"'"
}')
echo "$MY_OUTPUT"
}
and call it as:
for f in "/Users/anthony/my files"/*
do
commande_mine "$f"
break
done > test.txt

Can not to use POST in CURL

I am trying to make a script to use POST instead of GET but I not success at it.
This is the script:
#!/usr/bin/bash
echo "Content-type: application/json"
echo ""
login=`echo "$QUERY_STRING" | awk '{split($0,array,"&")} END{print array[1]}' | awk '{split($0,array,"=")} END{print array[2]}'`
clave=`echo "$QUERY_STRING" | awk '{split($0,array,"&")} END{print array[2]}' | awk '{split($0,array,"=")} END{print array[2]}'`
if curl -H "Content-Type: application/json" -d "username="$login"&passcode="$clave"" -X POST http://x.x.x.x/abc -s | grep -q mnp;
then
echo 1
else
echo 3
fi
the script works but it still using GET.
screenshot of HTTP request
Any ideas about the right format of the Curl command?
Regards

How to get dynamic NR parameter in AWK command at shell scripting

I have txt file like this:
1 a
2 b
3 c
I want to take these datas step by step for example first ı will get " 1 " and put it a varible and then get " a " put it in a varible and run a curl command
I mean first first row first column then fisrt row second column then second row first column .....
ı wrote a script in below but not working, it turns null varible for b and c
#!/bin/sh
for ((i=1;i<=4;i++)); do
echo $i
b=$(awk 'NR==$i { print $1}' a.txt)
c=$(awk 'NR==$i { print $2}' a.txt)
echo $b
echo $c
curl -X POST \
-H "X-netmera-api-key: xxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '[
{
"deviceToken" : "$b",
"platform" : "1",
"extId" : "$c"
}
]' \
https://xxxx.xxxxx.com/rest/3.0/xxxxx
done
Throw awk away; it isn't necessary here.
while read -r b c; do
curl -X POST \
-H "X-netmera-api-key: xxxxxxxxxx" \
-H "Content-Type: application/json" \
-d "[
{
\"deviceToken\" : \"$b\",
\"platform\" : \"1\",
\"extId\" : \"$c\"
}
]" \
https://xxxx.xxxxx.com/rest/3.0/xxxxx
done < a.txt
You shouldn't be trying to generate JSON dynamically like this, but that's an issue for another question.
You will need to pass variables to awk with -v and so:
b=$(awk -v varb=$i 'NR==varb { print $1}' a.txt)
Here we are setting the awk variable varb equal to the i variable of your bash loop.

Unable to send large files to elasticsearch using curl: argument too long

This is the script i used to export some documents to elasticsearch but no luck
#!/bin/ksh
set -v
trap read debug
date=$(date +%Y-%m-%d);
echo $date;
config_file="/home/p.sshanm/reports_elastic.cfg";
echo $config_file;
URL="http://p-acqpes-app01.wirecard.sys:9200/reports-"$date"";
echo $URL;
find /transfers/documents/*/done/ -type f -name "ABC-Record*_${date}*.csv"|
while IFS='' read -r -d '' filename
do
echo "filename : ${filename}"
var=$(base64 "$filename"| perl -pe 's/\n//g');
#if i use below it will fail as argument too long , so i used with curl option #
# var1= $(curl -XPUT 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -d' { "data" : "'$var'" }')
var1=$(curl -X PUT -H "Content-Type: application/json" -d #- "$URL" >>CURLDATA
{ "data": "$var" }
CURL_DATA)
done;
If i use below it as
var1= $(curl -XPUT 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -d' { "data" : "'$var'" }')
will fail as below, so i used with curl option #
argument too long
Your syntax to read from stdin is wrong, the here-doc string should have been (<<) and the de-limiters are mis-matching use CURL_DATA at both places.
curl -X PUT -H "Content-Type: application/json" -d #- "$URL" <<CURL_DATA
{ "data": "$var" }
CURL_DATA

Resources