如何使用Shell脚本将当前日期传递给curl查询?
我正在使用CURL将数据插入elasticsearch,当我插入固定数据时,它可以正常工作。我正在尝试获取当前DateTime并分配给变量,并与我要插入的对象一起使用。
这是我的剧本,
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并分配给CreateDate。我怎样才能做到这一点?
回答:
建议不要使用引号内的引号,而是建议使用 摆脱所有魔术引号。这样使用curl
:
number=10dt="$(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查询? 的全部内容, 来源链接: utcz.com/qa/414222.html