Odd and even date in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Odd and even date in bash
# 1  
Old 11-08-2013
Odd and even date in bash

Hi All,

I'm having the following script:

Code:
#!/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:

Code:
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

Code:
~ $ . test_even.sh 
08
date is odd

 $ date
Fri Nov  8 15:08:18 EET 2013

Please advise,thanks

Last edited by zarere; 11-08-2013 at 10:38 AM..
# 2  
Old 11-08-2013
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
Code:
echo $((10#${date}%2))

You can avoid the base 10 indicator if you use date +%e when assigning the variable.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-08-2013
Quote:
Originally Posted by 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
Code:
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:

Code:
echo $((10#${date}%2))

Code:
 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

Code:
. test_even.sh 
0
date is odd

# 4  
Old 11-08-2013
OK. echo $((10#${dates}%2)) obviously yields 0, if [ ${dates%2} -eq 0 ]; doesn't. What conclusion would YOU draw?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-08-2013
OK. echo $((10#${dates}%2)) obviously yields 0, if [ ${dates%2} -eq 0 ]; doesn't. What conclusion would YOU draw?

Obviously something in
Code:
[ ${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:

Code:
#!/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:
Code:
~ $ . test_even.sh 
0
date is even

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare date bash script

I all I have written a bash script for compare two date. One of those is a result of query, and another is current date. I have a problem with the format, because the first is 09/12/19 18:50:30 but for having this result I have to do d1DB=$(date -d "$valData" +'%m/%d/%y %T') and the second... (9 Replies)
Discussion started by: rdie77
9 Replies

2. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. UNIX for Dummies Questions & Answers

using date tool in bash

date --date='10:30am + 1 hour' +%H:%M 11:30 produces date --date='10:30pm + 1 hour' +%H:%M produces 23:30 I want to do the following: TIME="1:30pm" date --date='$TIME + 1 hour' + %H:%M to produce 14:30 (1 Reply)
Discussion started by: efittery
1 Replies

4. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

5. Shell Programming and Scripting

How to format string/date in bash?

I have string like "1-JUN-11" and it needs to be converted to "01JUN11" and "1/6/2011":confused::confused::confused: Thanks (2 Replies)
Discussion started by: ford99
2 Replies

6. Shell Programming and Scripting

Working with bash and date

Hello all, I'm trying to substract 1 minute from the current date and take the hour and minute (for filename purpose). 1) If I want hour an minute from current time I can use: timetmp=$(date +"%H:%M") 2) To substract 1 minute from current time I can use: timetmp=$(date --date "$dte -1... (8 Replies)
Discussion started by: Lord Spectre
8 Replies

7. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

8. Shell Programming and Scripting

date comparison in bash

Hi I have this simple script: #!/bin/bash date1=2009:07:15:12:36 date2=2009:07:15:12:16 echo $date1 echo $date2 datediff= #datediff=date1-date2 echo datediff is$datediff How do i return the difference in seconds? (6 Replies)
Discussion started by: carp.dk
6 Replies

9. UNIX for Dummies Questions & Answers

bash date format

hi guys. in bash is there any other way of limiting the time displayed to HH:MM appart from (date +"%H:%M") and (date +"%R")? i want to input time into a database in the form HH:MM have tried NOW() but this gives me HH:MM:SS thanks in advance (1 Reply)
Discussion started by: vadharah
1 Replies

10. UNIX for Dummies Questions & Answers

Bash comparing date

Cound anyone help me on how to compare date in Unix using if function on bash file? current=date if ###syntax is wrong, could anyone correct it for me then rm -rf /usr/local/src fi Thank You... (17 Replies)
Discussion started by: Stanford Co
17 Replies
Login or Register to Ask a Question