LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash error as [[: 08: value too great for base (error token is "08") (https://www.linuxquestions.org/questions/linux-newbie-8/bash-error-as-%5B%5B-08-value-too-great-for-base-error-token-is-08-a-4175465153/)

cli 06-07-2013 03:32 PM

bash error as [[: 08: value too great for base (error token is "08")
 
Hi All,
I am testing conditions with below script and wanted to run the commands on all of the day but only execpt the date of month 1st and except the day of sunday.
Code:

$ cat test1
#!/bin/bash
Today_Date=$(date '+%d')
Today_Week=$(date '+%w')
if  [[ $Today_Date -ne 1 || $Today_Week -gt 0 ]]; then
echo "Today is neither month 1st, nor sunday."
fi

changed date using mmddhhmmyyyy(month,date,hour,minutes,year)
Code:

sudo date 060100002013
$ date
Sat Jun  1 00:03:22 IST 2013
$ bash test
Today is neither month 1st, nor sunday.

Why?
Then I changed to Sunday date.
Code:

sudo date 060900002013
$ date
Sun Jun  9 00:00:06 IST 2013
$ bash test
Today is neither month 1st, nor sunday.

Why? Sometimes I'll get the below error also
Code:

[[: 08: value too great for base (error token is "08")
So please help me to run this script all of the day except month 1st and Sunday.

Thanks in adavance for your kind help.

rknichols 06-07-2013 04:02 PM

The leading zero makes "08" look like an octal number with an invalid digit. Either tell the date command to pad with spaces rather than zero ("date +%e" or "date +%_d"), or else explicitly identify the base when you use the value:
Code:

if  [[ 10#$Today_Date -ne 1 || $Today_Week -gt 0 ]]; then

cli 06-07-2013 04:15 PM

Thanks for the reply rknichols,
But still both methods are not working for me.
Code:

$ date
Sun Jun  9 00:09:48 IST 2013

$ cat test1
#!/bin/bash
Today_Date=$(date '+%e')
Today_Week=$(date '+%w')
if [[ $Today_Date -ne 1 || $Today_Week -gt 0 ]]; then
echo "Today is neither month 1st, nor sunday."
fi

$ bash test1
Today is neither month 1st, nor sunday.

$ cat test2
#!/bin/bash
Today_Date=$(date '+%d')
Today_Week=$(date '+%w')
if [[ 10#$Today_Date -ne 1 || $Today_Week -gt 0 ]]; then
echo "Today is neither month 1st, nor sunday."
fi

$ bash test2
Today is neither month 1st, nor sunday.

Please help.

mandyapenguin 06-07-2013 04:24 PM

Try
Code:

#!/bin/bash
Today_Date=$(date '+%e')
Today_Week=$(date '+%w')
if [[ $Today_Date -ne 1 && $Today_Week -gt 0 ]]; then
echo "Today is neither month 1st, nor sunday."
fi

Change the date to month 1st and also to Sunday, then try.

cli 06-07-2013 05:09 PM

Quote:

Originally Posted by mandyapenguin (Post 4967427)
Try
Code:

#!/bin/bash
Today_Date=$(date '+%e')
Today_Week=$(date '+%w')
if [[ $Today_Date -ne 1 && $Today_Week -gt 0 ]]; then
echo "Today is neither month 1st, nor sunday."
fi

Change the date to month 1st and also to Sunday, then try.

Thank you very much mandyapenguin. You both solved the problem.
Thanks a lot to LinuxQuestions.Org

David the H. 06-09-2013 05:08 AM

Read up here for more detail on how bash treats leading zeroes, and how to handle them:

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/018

Edit: BTW, you can combine your two date calls into one, like this:

Code:

read Today_Date Today_Week < <( date '+%e %w' )
Also, when using advanced shells like bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

Since these are numeric tests, you should be using:

Code:

if (( Today_Date != 1 && Today_Week > 0 )); then


All times are GMT -5. The time now is 01:31 AM.