Why didn’t the bash script work or about carriage return

I wrote my configuration file for Conky. I wanted to draw a conclusion of the dollar and the euro against the ruble and calculate the dynamics of the rates. The task is not difficult, so I quickly wrote a bash script. I decided to take exchange rates from the CBRF website .

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 -e s/\,/\./`
onedayagok=`cat onedayago.tmp | grep "USD"  -A3  | sed -n -e 4p | tr -d "A-Za-z<>/\t" | sed -e s/\,/\./`
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 started the script, I received an error message:

(standard_in) 1: illegal character: ^ M
(standard_in) 1: illegal character: ^ M




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


echo $nowk
echo $onedayagok

The output of the variables was correct:



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




Only -58.7710 came out. Where did 59.4452 go? Here difficulties arose. I decided to record the output of the operation to a 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 -e s/\,/\./ >1.txt`
onedayagok=`cat onedayago.tmp | grep "USD"  -A3  | sed -n -e 4p | tr -d "A-Za-z<>/\t" | sed -e s/\,/\./ >2.txt`

In appearance, everything was fine, the numbers were successfully written to the file.



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


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

Everything worked. It became obvious that the problem is in the data obtained. You just had to analyze the 2.txt and 3.txt files. 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. Using a search engine, we find that OD is "carriage return." That is, with the echo $ onedayagok- $ nowk command, the value of the onedayagok variable was first displayed, then from the beginning of the line, the nowk variable was displayed in the same line, that is, overriding the previous variable. Using the same search engine, we find out that to remove the "card" 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 -e s/\,/\./`
onedayagok=`cat onedayago.tmp | grep "USD"  -A3  | sed -n -e 4p | tr -d "A-Za-z<>/\t'\r" | sed -e s/\,/\./`
twodayagok=`cat twodayago.tmp | grep "USD"  -A3  |sed -n -e 4p | tr -d "A-Za-z<>/\t'\r'" `` 



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

Also popular now: