📜 ⬆️ ⬇️

Why bash script or carriage return did not work

I wrote my configuration file for Conky. I wanted to make a withdrawal of the dollar and the euro against the ruble and calculate the dynamics of the courses. The task is not complicated, so I quickly wrote a bash script. Exchange rates decided to take from the site of the CBRF .

The script turned out like this:

#!/bin/bash now=`date +%d/%m/%Y` onedayago=`date --date="1 day ago" +%d/%m/%Y` twodayago=`date --date="2 day ago" +%d/%m/%Y` wget -O now.tmp -q "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$now" wget -O onedayago.tmp -q "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$onedayago" wget -O twodayago.tmp -q "http://www.cbr.ru/scripts/XML_daily.asp?date_req=$twodayago" nowk=`cat now.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t'" | sed -es/\,/\./` onedayagok=`cat onedayago.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t" | sed -es/\,/\./` twodayagok=`cat twodayago.tmp | grep "USD" -A3 |sed -n -e 4p | tr -d "A-Za-z<>/\t" ` dinamika=`echo $onedayagok-$nowk | bc` echo $dinamika 


However, when I ran the script, I received the error message:
')
(standard_in) 1: illegal character: ^ M
(standard_in) 1: illegal character: ^ M



What's the matter? I decided to look at the variables separately. Added two lines:

 echo $nowk echo $onedayagok 

The output of the variables was correct:



Maybe a string error
 echo $onedayagok-$nowk 
? Add the output of this line with the command
 echo $onedayagok-$nowk 




Only -58.7710 was output. What happened to 59.4452? This is where the difficulties arose. I decided to record the output of the operation result in the file, added> 1.txt and> 2.txt after processing the data, that is, it turned out like this:

 nowk=`cat now.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t" | sed -es/\,/\./ >1.txt` onedayagok=`cat onedayago.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t" | sed -es/\,/\./ >2.txt` 

It looked fine, the numbers were successfully entered into the file.



Began to study a rather strange and unexpected problem further. I decided to create a file myself with the same content. Using the text editor nano, create a 3.txt file and enter 59.4452 into it. In the file 4.txt enter 58.7710. In the script, add the reading from the file, that is:

 nowk=`cat 3.txt` onedayagok=`cat 4.txt` 

Everything worked. It has become obvious that the problem is in the data obtained. You just had to analyze the files 2.txt and 3.txt. Next, open both files with a hex editor and find the very difference:



The files are almost identical, but 0D is present in the 2.txt file. With the help of a search engine we find that OD is “carriage transfer”. That is, with the echo $ onedayagok- $ nowk command, the value of the onedayagok variable was first displayed, then from the beginning of the line in the same line, the variable nowk was displayed, that is, it overlapped the previous variable. Using the same search engine, we learn that in order to remove the "map" we add '\ r' to the tr utility, that is, like this:

 nowk=`cat now.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t'\r'" | sed -es/\,/\./` onedayagok=`cat onedayago.tmp | grep "USD" -A3 | sed -n -e 4p | tr -d "A-Za-z<>/\t'\r" | sed -es/\,/\./` twodayagok=`cat twodayago.tmp | grep "USD" -A3 |sed -n -e 4p | tr -d "A-Za-z<>/\t'\r'" `` 



We are convinced that the script now works. The CBR returns this with a “carriage return.” That was the problem. Unfortunately, such nuances are not told in textbooks and articles on bash, so such difficulties may arise.

Source: https://habr.com/ru/post/254123/


All Articles