Reply
Thread Tools
Posts: 145 | Thanked: 302 times | Joined on Jan 2010 @ Milton Keynes, UK
#1
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

Last edited by jamiefuller; 2012-04-27 at 15:08.
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,404 times | Joined on Apr 2009 @ Germany
#2
08
Integers with leading "0" are interpreted as octal, so 08 is an illegal
octal number.
Remove the leading "0".

Nicolai
 

The Following 7 Users Say Thank You to nicolai For This Useful Post:
Posts: 145 | Thanked: 302 times | Joined on Jan 2010 @ Milton Keynes, UK
#3
Originally Posted by nicolai View Post
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
 
Posts: 701 | Thanked: 584 times | Joined on Sep 2010 @ London, England
#4
You can use bc to do the maths (you may need to install it)
Code:
	a=$(echo "( $a * 60 ) + $x" | bc )
 

The Following 8 Users Say Thank You to retsaw For This Useful Post:
Posts: 145 | Thanked: 302 times | Joined on Jan 2010 @ Milton Keynes, UK
#5
retsaw, that was perfect, thanks!
 
Posts: 245 | Thanked: 353 times | Joined on Jul 2010 @ Scotland
#6
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))
 

The Following 7 Users Say Thank You to gregoranderson For This Useful Post:
Posts: 701 | Thanked: 584 times | Joined on Sep 2010 @ London, England
#7
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.
 

The Following 3 Users Say Thank You to retsaw For This Useful Post:
Posts: 245 | Thanked: 353 times | Joined on Jul 2010 @ Scotland
#8
Originally Posted by retsaw View Post
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
 

The Following 2 Users Say Thank You to gregoranderson For This Useful Post:
Posts: 734 | Thanked: 1,372 times | Joined on Feb 2010 @ Gdynia, Poland
#9
Originally Posted by gregoranderson View Post
/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
 

The Following 6 Users Say Thank You to misiak For This Useful Post:
Posts: 701 | Thanked: 584 times | Joined on Sep 2010 @ London, England
#10
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.
 

The Following 3 Users Say Thank You to retsaw For This Useful Post:
Reply

Thread Tools

 
Forum Jump


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