maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Nokia N900 (https://talk.maemo.org/forumdisplay.php?f=44)
-   -   BASH script bug?, simple maths... (https://talk.maemo.org/showthread.php?t=83923)

jamiefuller 2012-04-27 15:02

BASH script bug?, simple maths...
 
Hi All,

I think i've found a little bug but would love for someone to confirm?

Basically I have this script
Code:

IN=00:08:19.12
IN=`echo $IN| cut -d'.' -f 1`

arr=$(echo $IN | tr ":" "\n")
a=0
for x in $arr
do
        echo $x
        a=$(( ( a * 60) + x ))
        echo $a
done

it should take IN and convert it into seconds.

This works on a unix box that I had access to but on the N900 I get the following error

Code:

Nokia-N900:/home/user/MyDocs# sh test
00
0
08
test: line 12: syntax error:  ( a * 60) + x

Could somebody recreate and confirm? or point out what I'm doing daft? =)

Thanks
jamie

UPDATE:

I've found that if I set IN to the following it works except for the lines in red italics? how wierd is that?

IN=00:00:19.12
IN=00:01:19.12
IN=00:02:19.12
IN=00:03:19.12
IN=00:04:19.12
IN=00:05:19.12
IN=00:06:19.12
IN=00:07:19.12
IN=00:08:19.12
IN=00:09:19.12

IN=00:10:19.12
IN=00:11:19.12
IN=00:12:19.12
IN=00:13:19.12
IN=00:14:19.12
IN=00:15:19.12
IN=00:16:19.12
IN=00:17:19.12
IN=00:18:19.12
IN=00:19:19.12
IN=00:20:19.12

nicolai 2012-04-27 15:19

Re: BASH script bug?, simple maths...
 
08
Integers with leading "0" are interpreted as octal, so 08 is an illegal
octal number.
Remove the leading "0".

Nicolai

jamiefuller 2012-04-27 15:32

Re: BASH script bug?, simple maths...
 
Quote:

Originally Posted by nicolai (Post 1198248)
08
Integers with leading "0" are interpreted as octal, so 08 is an illegal
octal number.
Remove the leading "0".

Nicolai

Thanks Nicolai,

any chance of a code snipet as to how to remove the trailing zero?

Or just a better way of doing what I'm doing =)

Thanks
Jamie

retsaw 2012-04-27 15:37

Re: BASH script bug?, simple maths...
 
You can use bc to do the maths (you may need to install it)
Code:

        a=$(echo "( $a * 60 ) + $x" | bc )

jamiefuller 2012-04-27 15:52

Re: BASH script bug?, simple maths...
 
retsaw, that was perfect, thanks!

gregoranderson 2012-04-27 16:40

Re: BASH script bug?, simple maths...
 
Following on from Nicolai's explanation - an alternative to using bc is to force bash to interpret the value as decimal, thus having the added option of the leading zero being chopped:

Code:

#!/bin/bash

LIFE=042

# Will return 34 as LIFE is interpreted as octal
echo $((LIFE))

# Force decimal representation
LIFE=$((10#${LIFE}))

# Should return the proper meaning of life
echo $((LIFE))


retsaw 2012-04-27 16:58

Re: BASH script bug?, simple maths...
 
Which while it works with bash, the N900 by default uses the ash shell built in to busybox and it doesn't work with that.

gregoranderson 2012-04-27 17:06

Re: BASH script bug?, simple maths...
 
Quote:

Originally Posted by retsaw (Post 1198292)
Which while it works with bash, the N900 by default uses the ash shell built in to busybox and it doesn't work with that.

/me facepalms.

Fair point, well made! Apologies :)

misiak 2012-04-27 18:07

Re: BASH script bug?, simple maths...
 
Quote:

Originally Posted by gregoranderson (Post 1198297)
/me facepalms.

Fair point, well made! Apologies :)

But the title of this threas states explicitly that this question is about bash, so no need to facepalm ;) Unless for OP "any shell"=="bash" which is a common misunderstanding (and a reason to facepalm for OP), but I doubt it ;)

retsaw 2012-04-27 18:32

Re: BASH script bug?, simple maths...
 
That point hadn't passed me by, but I also noted the error message was from Busybox's Ash shell as Bash gives a slightly different error. But I agree, there's no need for the facepalm, it was a useful post nonetheless and Bash was mentioned in the title.


All times are GMT. The time now is 21:30.

vBulletin® Version 3.8.8