0

I wanted to ask if my bash script is right. I want to send the output of the sensors command every 1 second, for 10 times:

#!/bin/bash
for i in {1..10}
do
a=$(date '+%s')
D=$(sensors)

curl -i -k -X POST -H "Content-Type: application/json" --data '{ "id":2,"timestamp":echo "${a}","data":echo "${D}"}' https://dv7knsjzph.execute-api.eu-central-1.amazonaws.com/prod/boxtronic-devices/2/data/
sleep 1
done

Thank you

8
  • You are missing done at the end of the script.
    – nobody
    Commented Nov 10, 2020 at 11:29
  • Yes I added it but I wanted to ask if the syntax is right. I want to upload the content of a and D every second to my server. Is for example "timestamp": "echo "${a}" or "timestamp": echo "${a}" the right syntax Commented Nov 10, 2020 at 11:32
  • I think you should delete echo.
    – nobody
    Commented Nov 10, 2020 at 11:39
  • can you write an example of the syntaxe ? which one of the two. Try it out of the script. deleting it prints an incomplete output Commented Nov 10, 2020 at 11:40
  • 1
    curl -i -k -X POST -H "Content-Type: application/json" --data "{ \"id\":2,\"timestamp\": \"${a}\",\"data\": \"${D}\"}" https:......
    – mfischer
    Commented Nov 10, 2020 at 11:46

2 Answers 2

1

Don't use echo when calling a command.

You can either refrain from using single quotes. If you choose to do so you need to escape the quotes in your command call:

curl -i -k -X POST -H "Content-Type: application/json" --data "{ \"id\":2,\"timestamp\": \"${a}\",\"data\": \"${D}\"}" https:......

Or, if you want to use single quotes, you have to do string concatenation, that means end your single quotes before your variable and continue them afterwards (as "nobody" suggested:

curl -i -k -X POST -H "Content-Type: application/json" --data '{ "id":2,"timestamp": "'"${a}"'","data": "'"${D}"'"}' https:......

If you insist to use echo, you need to use echo's output in your call:

curl -i -k -X POST -H "Content-Type: application/json" --data "{ \"id\":2,\"timestamp\": \"$(echo ${a})\",\"data\": \"$(echo ${D})\"}" https:......
0
0

First of all, your loop syntax is wrong. for i in 1 .. 10 will run three times. Once for 1, once for .. and once for 10. To get the numbers between one and ten, you need for i in {1..10}. Next, use single quotes inside your curl command so you can still use double quotes on the outside so that variables will be expanded. Something like this:

#!/bin/bash
for i in {1..10}
do
    a=$(date '+%s')
    D=$(sensors)

    curl -i -k -X POST -H "Content-Type: application/json" \
        --data "{ 'id':2,'timestamp':$a,'data':$D}" https:......
    sleep 1
done

Or, if you need both the tag name and the value to be quoted, use:

#!/bin/bash
for i in {1..10}
do
    a=$(date '+%s')
    D=$(sensors)

    curl -i -k -X POST -H "Content-Type: application/json" \
        --data "{ 'id':'2','timestamp':'$a','data':'$D'}" https:......
    sleep 1
done
8
  • I get the following error ``` {"message": "Internal server error"}HTTP/1.1 502 Bad Gateway ``` Using a simple syntax like this one does not trigger the error ``` curl -i -k -X POST -H "Content-Type: application/json" --data '{ "id":"id","timestamp":"data","data":"data"}' ``` Commented Nov 10, 2020 at 12:45
  • @Spearitch502 since you haven't given us a valid URL, I have no way of testing and I don't know what format is expected. I also don't know which of my two suggestions you are using. Does the second one fail as well? That one should work since everything is quoted. Finally, please edit your question and add what you tried and the error you get,. As you have seen, it is very hard to add formatting to comments. Just edit the question so we can all see what you tried.
    – terdon
    Commented Nov 10, 2020 at 12:48
  • My bad .. I added the URL in the top Commented Nov 10, 2020 at 12:49
  • @Spearitch502 and did you try the second option from my answer?
    – terdon
    Commented Nov 10, 2020 at 13:19
  • Yes I tried it but still nothing. Can you try it again? There may be a problem in the server Commented Nov 10, 2020 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .