Can you please help me?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can you please help me?
# 1  
Old 07-09-2008
Can you please help me?

Hi,

Can you please help me? I am new in unix scripting, can you please help me? I need to get the current system date and from it, i need to deduct 5-days from that current system date and to put it in a variable. From that variable, i need to identify or get what is the "DAY" value of that deducted date.


script is like this:
curdate=`date +%Y%m%d`
deddate=`expr $curdate - 5`
echo $curdate
echo $deddate

output is:
20080710
20080705


....so, how can i get the "DAY" value of that deddate? (is it fall on sunday, monday, tuesday, etc....)


Thanks in advance for the help. I need it badly.
# 2  
Old 07-09-2008
Are you using Linux? If so the date command has many useful functions for this, e.g.

Code:
date --date '5 days ago' +%A

Your method will not work very well for dates such as 20080701 (which would become 20080696, obviously not a valid date).
# 3  
Old 07-09-2008
Hi Annihilannic,

I'm using unix scripting.

another way is made is like this:
[hkdux116]:/home/carolyn> x=`date +%A`
[hkdux116]:/home/carolyn> echo $x
Thursday

....but what if i like to less 5-days from $x ? How can i get the day value? Please help me?

Thanks.
# 4  
Old 07-09-2008
You could use/modify this perl snippet I wrote to obtain the day of the week of an arbitrary date. It will accept negative values (because the mktime() function corrects for this), so if you just subtract 5 from the current day of the month it should still work.

Code:
#!/usr/bin/perl
#
# dayofweek.pl - Returns the day of week for a given MM DD YYYY combination.

use POSIX;
$time_t = POSIX::mktime( 0, 0, 0, $ARGV[1], $ARGV[0]-1, $ARGV[2]-1900);
printf "%s\n",strftime("%A",localtime($time_t));

e.g.

Code:
x=$(./dayofweek.pl $(date +%m) $(( $(date +%d) - 5 )) $(date +%Y) )

Apologies for the American date format of the parameters, that was what the person who I wrote the script for wanted. :-)
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question