如何使用Shell脚本将当前日期传递给curl查询? [英] How to pass current date to a curl query using shell script?
本文介绍了如何使用Shell脚本将当前日期传递给curl查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用CURL将数据插入弹性搜索,当我插入固定数据时,它可以正常工作。我试图获取当前DateTime并分配给变量,并与我要插入的对象一起使用。
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.
这是我的脚本,
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"
}
]
我需要获取当前的DateTime d分配给CreateDate。我该怎么办?
I need to get the current DateTime and assign to the CreateDate. How can i do that?
推荐答案
与其建议不要在引号内使用引号,不建议使用 here-doc 强>摆脱所有魔术报价。像这样使用您的 curl
:
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
这篇关于如何使用Shell脚本将当前日期传递给curl查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文