dj_janker
Posts: 59
Joined: Thu Jan 08, 2015 6:15 pm

Strange behavior bash script

Fri May 06, 2016 5:45 am

Hi, I have this bash script running without problems in one Rpi since several months ago.

Code: Select all

#!/bin/bash

base="http://192.168.1.13:80/core/api/jeeApi.php?api="
key="xxxxxxxxxxx"
type="virtual"
diaactual=`date +%d`
mesactual=`date +%m`
anyoactual=`date +%Y`

watios=$(sqlite3 -csv eagleowl.db "select year||'-'||substr('0'||month,-2,2)||'-'||substr('0'||day,-2,2)||' '||substr('0'||hour,-2,2)||':'||substr('0'||min,-2,2) date,$
a="$(echo "$watios" | cut -c 20-)"
echo $a
c="$(echo $watios| cut -c2-11)"
echo $c

intensidad=$(sqlite3 -csv eagleowl.db "select year||'-'||substr('0'||month,-2,2)||'-'||substr('0'||day,-2,2)||' '||substr('0'||hour,-2,2)||':'||substr('0'||min,-2,2) d$
b="$(echo "$intensidad" | cut -c 20-)"
echo $b
hora=$(sqlite3 -csv eagleowl.db "select hour||':'||substr('0'||min,-2,2) date,cast(round(60*ch1_kw_avg) as int) w from energy_history where year=$anyoactual and month=$
#print $medida
d="$(echo $hora | cut -c1-4)"
echo $d
urla="$base$key&type=$type&id=5999&value=$a"
URLa=${urla%$'\r'}
echo $URLa
curl $URLa
The problem is that I´m trying to use the same script [simple copy and paste] in other Rpi, but in this case I receive this error:

Code: Select all

watios2.sh: line 23: unexpected EOF while looking for matching `"'
watios2.sh: line 28: syntax error: unexpected end of file
I think that the problem isn´t in the text, beacause is the same, but I can´t find the error..... :cry:

Any suggestion?

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Strange behavior bash script

Fri May 06, 2016 6:14 am

Have you got a link to where you copy/pasted that script from?

The list you show has lines that are truncated (bits are missing). Those are the lines with a $ on the end.

User avatar
Cancelor
Posts: 775
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Strange behavior bash script

Fri May 06, 2016 6:58 am

I notice that when I copy your code sample that the following characters are used as quotes ` and '
I think the first character (`) is wrong and the second character (') is right. This part of the code uses the wrong character

Code: Select all

diaactual=`date +%d`
mesactual=`date +%m`
anyoactual=`date +%Y`
it should be like this

Code: Select all

diaactual='date +%d'
mesactual='date +%m'
anyoactual='date +%Y'
There may be more places where the wrong character is used.
Copy/Paste/Putty/Windows/Linux/HTTP doesn't always do what we want ;)

P.S. There are also at least two types of " but I think you are okay with this.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Strange behavior bash script

Fri May 06, 2016 4:40 pm

Cancelor wrote:I notice that when I copy your code sample that the following characters are used as quotes ` and '
I think the first character (`) is wrong and the second character (') is right. This part of the code uses the wrong character

Code: Select all

diaactual=`date +%d`
mesactual=`date +%m`
anyoactual=`date +%Y`
No, the backtick is right, it's used to set the variable to the output of the command. Although $() is generally preferable as it nests better. Although dj_janker also uses that method later on.
She who travels light — forgot something.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Strange behavior bash script

Fri May 06, 2016 5:04 pm

rpdom wrote:Have you got a link to where you copy/pasted that script from?

The list you show has lines that are truncated (bits are missing). Those are the lines with a $ on the end.
I wrapped the code in a function without calling the function and ran it through bash. Each of those three lines has the same error. So you are exactly right. It's bad form to have lines going so far off the screen that your copy and paste facility barfs on them.

dj_janker
Posts: 59
Joined: Thu Jan 08, 2015 6:15 pm

Re: Strange behavior bash script

Sat May 07, 2016 3:52 pm

Yes, you have reason. Some lines are too long and didn´t copy completly. :oops: :oops:
Finaly I used Filezilla to copy the file and the problem solved.

Thanks.

Best regards

Return to “General programming discussion”