How to pass current date to a curl query using shell script?
Asked Answered
C

3

12

I am inserting data to a elastic search using CURL, it works fine when i insert with a fixed data. I am trying to Get Current DateTime and assign to a variable and use with the object that i want to insert.

Here is my Script,

while true;
do

echo $i

number=$RANDOM;
let "number %= 9";
let "number = number + 1";
range=10;
for i in {1..18}; do
  r=$RANDOM;
  let "r %= $range";
  number="$number""$r";
done;

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'+$number+'' -d '{
  "CustType": null,
  "tag": "OrderType:Postpaid",
  "GUDeviceID": "0",
  "IsAvailable": false,
  "GUOrderID": "123",
  "OrderID": "3",
  "OrderDate": "2015-01-06T15:23:42.7198285+05:30",
  "GUAccountID": "15010615234251403",
  "CreateUser": "admin",
  "CreateDate": "2015-01-01T15:23:42",
  "CancelledDate": "1899-01-01T00:00:00",
  "CancelledUser": null,
  "GUTranID": "15010615234271604",
  "TenentID": 39,
  "CompanyID": 42,
  "ViewObjectID": 0,
  "ObjectID": null,
  "Status": 2,
  "OrderDetails": [
    {
      "GUPromtionID": "15010519341113508",
      "GUOrderID": "15010615234271703",
      "ChangeID": 0,
      "GUPackageID": "14100112243589402",
      "startdate": "2015-01-06T00:00:00" 
    }
]

I need to get the current DateTime and assign to the CreateDate. How can i do that?

Cheffetz answered 19/5, 2015 at 13:59 Comment(0)
W
6

Rather than playing with quotes inside quotes I would suggest using here-doc to get rid of all magic quoting. Use your curl like this:

number=10
dt="$(date --iso-8601=seconds)"

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'$number -d@- <<EOF
{
  "CustType": null,
  "tag": "OrderType:Postpaid",
  "GUDeviceID": "0",
  "IsAvailable": false,
  "GUOrderID": "123",
  "OrderID": "3",
  "OrderDate": "2015-01-06T15:23:42.7198285+05:30",
  "GUAccountID": "15010615234251403",
  "CreateUser": "admin",
  "CreateDate": "$dt",
  "CancelledDate": "1899-01-01T00:00:00",
  "CancelledUser": null,
  "GUTranID": "15010615234271604",
  "TenentID": 39,
  "CompanyID": 42,
  "ViewObjectID": 0,
  "ObjectID": null,
  "Status": 2,
  "OrderDetails": [
    {
      "GUPromtionID": "15010519341113508",
      "GUOrderID": "15010615234271703",
      "ChangeID": 0,
      "GUPackageID": "14100112243589402",
      "startdate": "2015-01-06T00:00:00"
    }
  ]
}
EOF
Well answered 19/5, 2015 at 14:39 Comment(0)
P
17

Inside your string, change

"CreateDate": "2015-01-01T15:23:42",

to

"CreateDate": "'"$(date +%Y-%m-%dT%H:%M:%S)"'",

There, I terminated the ' string and started a " string with the $(date) inside it. Otherwise, it would not get executed, but just passed to curl as a string.

You can also assign it to a variable beforehand and use it later like this:

now=$(date +%Y-%m-%dT%H:%M:%S)

...

"CreateDate": "'"$now"'",

Other problems

Change

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'+$number+'' -d '{

into

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'"$number" -d '{

Bash concatenation is just two strings one after another without a space between them. Otherwise, it would query URLS like http://localhost:9200/nondomain_order/orders/+0123456789+ instead of http://localhost:9200/nondomain_order/orders/0123456789

(Here, I protected the numbervariable against expansion with double quotes for safety if it ever changes)

Plumule answered 19/5, 2015 at 14:4 Comment(2)
how does this answers my question?Cheffetz
Sorry, I expected the number to be the date. Here is the answer, using @Val 's date format.Plumule
W
6

Rather than playing with quotes inside quotes I would suggest using here-doc to get rid of all magic quoting. Use your curl like this:

number=10
dt="$(date --iso-8601=seconds)"

curl -XPUT 'http://localhost:9200/nondomain_order/orders/'$number -d@- <<EOF
{
  "CustType": null,
  "tag": "OrderType:Postpaid",
  "GUDeviceID": "0",
  "IsAvailable": false,
  "GUOrderID": "123",
  "OrderID": "3",
  "OrderDate": "2015-01-06T15:23:42.7198285+05:30",
  "GUAccountID": "15010615234251403",
  "CreateUser": "admin",
  "CreateDate": "$dt",
  "CancelledDate": "1899-01-01T00:00:00",
  "CancelledUser": null,
  "GUTranID": "15010615234271604",
  "TenentID": 39,
  "CompanyID": 42,
  "ViewObjectID": 0,
  "ObjectID": null,
  "Status": 2,
  "OrderDetails": [
    {
      "GUPromtionID": "15010519341113508",
      "GUOrderID": "15010615234271703",
      "ChangeID": 0,
      "GUPackageID": "14100112243589402",
      "startdate": "2015-01-06T00:00:00"
    }
  ]
}
EOF
Well answered 19/5, 2015 at 14:39 Comment(0)
E
5

You can do it like this:

DATE_ISO=$(date +"%Y-%m-%dT%H:%M:%S")
...
curl -XPUT 'http://localhost:9200/nondomain_order/orders/'+$number+'' -d '{
...
   "CreateDate": "'"$DATE_ISO"'",
...
}'
Exemplum answered 19/5, 2015 at 14:4 Comment(5)
This will not work, $DATE_ISO is not expanded inside single-quoted strings. you should use "CreateDate": "'"$DATE_ISO"'",Plumule
You're right. Edited per @Camusensei' s comment, thanks!Exemplum
Also, it is bad practice to write variable/constants in uppercase, as it might overwrite a bash-internal variable. Here, DATA_ISO is not overwritten, but you might have a bad surprise someday if you continue testing your luck :DPlumule
Yeah, well... ok ;-)Exemplum
Did not work for me in emulator: #47837152Lizliza

© 2022 - 2024 — McMap. All rights reserved.