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

后端 未结 3 1315
清歌不尽
清歌不尽 2021-02-14 13:21

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

3条回答
  •  长情又很酷
    2021-02-14 14:21

    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)

提交回复
热议问题