Converting date +%j into integer

Dear community,

i got a problem to get "date +%j" as the right value.
Today is the 10th day of the year.

#./script.sh 2

#!/bin/bash/
Var1=$(date +%j)
Var2=$1

let result=$Var1+$Var2
echo $Var1 plus $Var2 equals $result

The output of the script is:

010 plus 2 equals 10
010 plus 3 equals 11
and so on.

I tried to declare $Var1 as an integer.

typeset -i Var1 Var2

The output after the declaration is

8 plus 2 equals 10
8 plus 3 equals 11
and so on.

The result should be 12. I need it for further calculations.

do you got any suggestion or solution for this problem?

sincerely

Oskar

a bug in bash??

n12:/home/vbe/wks/test/arithmetics $ bash
n12:/home/vbe/wks/test/arithmetics $ let A=$(date +%j)
n12:/home/vbe/wks/test/arithmetics $ echo $A
8
n12:/home/vbe/wks/test/arithmetics $ exit
exit
n12:/home/vbe/wks/test/arithmetics $ echo $0
/usr/dt/bin/dtksh
n12:/home/vbe/wks/test/arithmetics $ let A=$(date +%j)
n12:/home/vbe/wks/test/arithmetics $ echo $A              
10

Did my test on AIX7.1 after seeing on old HP it works (only ksh...)

So change your first line to sh or ksh...

1 Like

ok quick update:

n12:/home/vbe/wks/test/arithmetics $ bash
n12:/home/vbe/wks/test/arithmetics $ date +%j
010
n12:/home/vbe/wks/test/arithmetics $ let A=$(date +%j)
n12:/home/vbe/wks/test/arithmetics $ echo $A
8
n12:/home/vbe/wks/test/arithmetics $  A=$(date +%j)
n12:/home/vbe/wks/test/arithmetics $ echo $A
010
n12:/home/vbe/wks/test/arithmetics $ let B=$A
n12:/home/vbe/wks/test/arithmetics $ echo $B
8
 
Only workaround for now ( I have other things to do...) I can think of is:
n12:/home/vbe/wks/test/arithmetics $ echo "$A + $B" |bc
12

thank you, i had to install ksh first, but now it works fine!

:b::b::b:

This is not a bug -- the leading 0 causes the shell to think the number is octal. 010 would be 8 in octal.

You can do string operations to strip off leading 0's, but I tend to take the easy way, slapping a '1' on the front to make it look decimal. Subtract 1000 as part of your math operations.

1 Like

You could also user typeset to force the variable to be an integer:-

typeset -i Var1=$(date +%j)

I have noticed a difference in the output from this depending on the OS though.

It seems that AIX (4, 5 and 6) & HP-UX 11 do what I expect, but RHEL (bash & ksh93) computes this to set Var=11

Maybe it's to do with the version of the shell, but AIX 6 has ksh93 available and that works as I expect (value of 13). Something to be tested first. Maybe this would be safer:-

Var1=`date +%j`
Var1="${Var1##*0}"

Robin

@rbatte that last bit will not work for day of the year with a 0 in position 2 or 3. Perhaps this:

Var1=${Var1#"${Var1%%[!0]*}"}

Or make bash accept it as a decimal value by preceding it with the base 10# indicator:

let result=10#$Var1+$Var2
echo $Var1 plus $Var2 equals $result
014 plus 2 equals 16

Dear Scrutinizer,

I am a fool. How did I not think of that? :confused:

Perhaps I should have done a quick loop to test it out too. How embarrassing. :o

I suppose I could trim the possible first zero twice like this:-

typeset -Z3 testdate=1
while [ $testdate -le 370 ]
do
   Var1="${testdate#0}"            # Trim first zero only if it exists
   Var1="${Var1#0}"                # Trim off a second zero if that exists
   echo $testdate $Var1
   ((testdate=$testdate+1))
done

Is that more expensive? It's a little easier to read than yours, but I'm prepared to be converted if there is a cost, especially as people may want to use this in a loop for all sorts of things.

Robin

Haha, you're being too hard on yourself :slight_smile: I agree your suggestion looks more readable and I don't think it is more expensive.

1 Like