0

I have a python code that will send a HTTP request to a webserver to update its time according to PC time and this code works fine:

current_time = str(time.time())
url = "http://192.168.1.1/cgi-bin/Operation.cgi?data={\"action\":\"synctime\",\"newtime\":" + current_time + "}"
response = urllib.request.urlopen(url)

Then I'm trying to translate this code into a bash script to do the same thing and this is what I have:

current_time=$(date +%s)
url="http://192.168.1.1/cgi-bin/Operation.cgi?data={\"action\":\"synctime\",\"newtime\":$current_time}"
response=$(curl -s "$url")

I'm echoing result from the URL variable and it looks like this:

}ttp://192.168.1.1/cgi-bin/Operation.cgi?data={"action":"synctime","newtime":1681742302

There's a closing curly braces in the beginning of this string replacing the "h" letter and I don't understand why the output being like this. I tried to change the format to use single quotes and also concatenate the current_time variable in url variable but nothing seems to work.

Can someone please explain why this curly braces is where it is and what can I do to fix it?

Maluco
  • 43
  • 7
  • 2
    Your file has dos line endings. Remove them. – KamilCuk Apr 17 '23 at 14:49
  • @executable this is exactly what I did. Take a look at the code I published. – Maluco Apr 17 '23 at 14:57
  • @KamilCuk I'm using notepad++ to avoid this mistake. Would you recommend a different editor or something that I can use to remove dos line endings? – Maluco Apr 17 '23 at 14:59
  • 3
    Notepad++ will not "protect" you against dos line endings, no editor will. https://stackoverflow.com/questions/16239551/eol-conversion-in-notepad – KamilCuk Apr 17 '23 at 15:01
  • 1
    @KamilCuk you were right on target. Checked this topic about eol conversion and using Notepad++ > Edit > EOL Conversion > Unix (LF) did the job! Thank you very much for your help! – Maluco Apr 17 '23 at 15:10

0 Answers0