Solved remove preceded "0" from date's output.

Hei.

I want to calculate with the output of date "+%W", which is the week of the year.
As it is the 8th week of the year the output is currently "08" but I need it to be just "8".

How can I cut off the "0"?

Made the .sh script last year and it works with the week of year beeing at least 10.

The script doesn't do any more then finding if the week is even or odd and it look like this:
Code:
if [ $((`date "+%W"`%2)) -eq 0 ];then echo even > /var/cron/week_of_year
else echo odd > /var/cron/week_of_year
fi

Now the script gives the following error:
Code:
./week_of_year.sh: arithmetic expression: expecting EOF: "08%2"

Changing the script in any way would also do, but I don't know how.

Help appreciated :)
 
Hei.

I want to calculate with the output of date "+%W", which is the week of the year.
As it is the 8th week of the year the output is currently "08" but I need it to be just "8".

How can I cut off the "0"?

Made the .sh script last year and it works with the week of year beeing at least 10.

The script doesn't do any more then finding if the week is even or odd and it look like this:
Code:
if [ $((`date "+%W"`%2)) -eq 0 ];then echo even > /var/cron/week_of_year
else echo odd > /var/cron/week_of_year
fi

Now the script gives the following error:
Code:
./week_of_year.sh: arithmetic expression: expecting EOF: "08%2"

Changing the script in any way would also do, but I don't know how.

Help appreciated :)

Change date "+%W" with date "+%W" | sed 's/^0//'. In this way the 0 will be removed if it's the first character. Your shell is complaining about it.
 
Thank you mate, you made my day :)
Code:
if [ $((`date "+%W" | sed 's/^0//'`%2)) -eq 0 ];
Works like a charm!
 
Back
Top