Linux Bash Scripting Again....
Author
Discussion

TheExcession

Original Poster:

11,669 posts

277 months

Thursday 28th October 2004
quotequote all
Bit stuck with a small problem:

I pass a date into my shell script as say 20040901 - 1st Sept 2004 - as STARTDATE

Then
declare -i STARTYEAR="${STARTDATE:0:4}"
declare -i STARTMONTH="${STARTDATE:4:2}"
declare -i STARTDAY="${STARTDATE:6:2}"

trouble is STARTMONTH gets forced into base 8 due to the leading zero on '09' and then unsurprisingly gives a value too large for base error.

Is their a quick easy way to ensure I get a decimal value for '08' and '09'? Similar problem occurs with some of the day values too...

many thanks
Ex

GregE240

10,857 posts

294 months

Thursday 28th October 2004
quotequote all
TheExcession said:
Bit stuck with a small problem:

I pass a date into my shell script as say 20040901 - 1st Sept 2004 - as STARTDATE

Then
declare -i STARTYEAR="${STARTDATE:0:4}"
declare -i STARTMONTH="${STARTDATE:4:2}"
declare -i STARTDAY="${STARTDATE:6:2}"

trouble is STARTMONTH gets forced into base 8 due to the leading zero on '09' and then unsurprisingly gives a value too large for base error.

Is their a quick easy way to ensure I get a decimal value for '08' and '09'? Similar problem occurs with some of the day values too...

many thanks
Ex
Ex, maybe I'm barking up the wrong tree, but are you sure about base 8?

Its just that 9 in base 8 is 11?

Greg

TheExcession

Original Poster:

11,669 posts

277 months

Thursday 28th October 2004
quotequote all
GregE240 said:



TheExcession said:
Bit stuck with a small problem:

I pass a date into my shell script as say 20040901 - 1st Sept 2004 - as STARTDATE

Then
declare -i STARTYEAR="${STARTDATE:0:4}"
declare -i STARTMONTH="${STARTDATE:4:2}"
declare -i STARTDAY="${STARTDATE:6:2}"

trouble is STARTMONTH gets forced into base 8 due to the leading zero on '09' and then unsurprisingly gives
a value too large for base error.


Is their a quick easy way to ensure I get a decimal value for '08' and '09'? Similar problem occurs with some of the day values too...

many thanks
Ex



Ex, maybe I'm barking up the wrong tree, but are you sure about base 8?

Its just that 9 in base 8 is 11?

Greg




true base 8 goes:
0,1,2,3,4,5,6,7,10,11,12,13,14,15,16,17,20,21....

but when bash sees a leading zero on a value and assigns it to a script variable the leading zero is interepetted as being an instruction to force into base 8 - which throws an error - I need to force it into base 10 -

or write something to drop the leading zero before doing the assignment.

Just need sumfin quick and simple and not sure what to do.

thanks
Ex

>> Edited by TheExcession on Thursday 28th October 10:30

zumbruk

7,848 posts

287 months

Thursday 28th October 2004
quotequote all
The real problem is that you're trying to do implicit ASCII to integer conversion and bash doesn't understand what you mean.

Unfortunately, I can't really help you, 'cos I write anything that requires that kind of functionality in Perl.

Sorry.

ATG

23,522 posts

299 months

Thursday 28th October 2004
quotequote all
horrific, but can you concatenate a leading 1 onto your string, convert that to an integer, then subtract 100?

zumbruk

7,848 posts

287 months

Thursday 28th October 2004
quotequote all
Slightly less horrifically, can you catenate "10#" onto the beginning of your string which specifies the radix of the number?

(Works for any base, BTW.)

TheExcession

Original Poster:

11,669 posts

277 months

Thursday 28th October 2004
quotequote all
zumbruk said:
Slightly less horrifically, can you catenate "10#" onto the beginning of your string which specifies the radix of the number?

(Works for any base, BTW.)


Thanks zumbruk, that works a treat:
declare -i STARTMONTH="10#${STARTDATE:4:2}" etc...

best
Ex

zumbruk

7,848 posts

287 months

Thursday 28th October 2004
quotequote all
You're welcome. No worries.