Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value too great for base (error token is "0925")

Tags:

bash

ubuntu

octal

I have the following logic in my bash script:

#!/bin/bash
local_time=$(date +%H%M)

if (( ( local_time > 1430  && local_time < 2230 ) || ( local_time > 0300 && local_time < 0430 ) )); then
 # do something
fi

Every now and then, I get the error specified in the title (any time above 08xx appears to trigger the error).

Any suggestions on how to fix this?

I am running on Ubuntu 10.04 LTS

[Edit]

I modified the script as suggested by SiegeX, and now, I am getting the error: [: 10#0910: integer expression expected.

Any help?

like image 783
oompahloompah Avatar asked Mar 28 '11 07:03

oompahloompah


1 Answers

bash is treating your numbers as octal because of the leading zero

From man bash

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 represent- ing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used.

To fix it, specify the base-10 prefix

#!/bin/bash
local_time="10#$(date +%H%M)"

if (( ( local_time > 1430  && local_time < 2230 ) || ( local_time > 0300 && local_time < 0430 ) )); then
 # do something
fi
like image 93
SiegeX Avatar answered Sep 25 '22 03:09

SiegeX