zarere
November 8, 2013, 8:09am
1
Hi All,
I'm having the following script:
#!/bin/bash
date=$(date +%d)
echo $date
if [ ${date%2} -eq 0 ];
then
echo 'date is even'
else
echo 'date is odd'
fi
but I can not find out why it's not working.Example:
cat test_even.sh
#!/bin/bash
date=$(date +%d)
echo $date
if [ ${date%2} -eq 0 ];
then
echo 'date is even'
else
echo 'date is odd'
fi
~ $ . test_even.sh
08
date is odd
$ date
Fri Nov 8 15:08:18 EET 2013
Please advise,thanks
RudiC
November 8, 2013, 8:15am
2
You should use bash's integer arithmetics. And, btw, using a variable with the same name as a command works, but can become veeery misleading sometime! Try
echo $((10#${date}%2))
You can avoid the base 10 indicator if you use date +%e
when assigning the variable.
1 Like
zarere
November 8, 2013, 9:35am
3
rudic:
You should use bash's integer arithmetics. And, btw, using a variable with the same name as a command works, but can become veeery misleading sometime! Try
echo $((10#${date}%2))
You can avoid the base 10 indicator if you use date +%e
when assigning the variable.
Hi RudiC,
10x for the information but that still don't work even with:
echo $((10#${date}%2))
cat test_even.sh
#!/bin/bash
dates=$(date +%d)
echo $((10#${dates}%2))
if [ ${dates%2} -eq 0 ];
then
echo 'date is even'
else
echo 'date is odd'
fi
. test_even.sh
0
date is odd
RudiC
November 8, 2013, 9:41am
4
OK. echo $((10#${dates}%2))
obviously yields 0, if [ ${dates%2} -eq 0 ];
doesn't. What conclusion would YOU draw?
1 Like
zarere
November 8, 2013, 9:56am
5
OK. echo $((10#${dates}%2))
obviously yields 0, if [ ${dates%2} -eq 0 ];
doesn't. What conclusion would YOU draw?
Obviously something in
[ ${dates%2} -eq 0 ];
is not correct or I'm missing something?
---------- Post updated at 04:56 PM ---------- Previous update was at 04:50 PM ----------
OK I got it now:
#!/bin/bash
dates=$(date +%d)
echo $((10#${dates}%2))
if [ $((10#${dates}%2)) -eq 0 ];
then
echo 'date is even'
else
echo 'date is odd'
fi
Now it's ok my bad and thanks again for the help:
~ $ . test_even.sh
0
date is even