Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-29-2010
Registered User
 

Join Date: Feb 2010
Location: Hawaii
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help!


Code:


yy=`date +%Y`
mm=`date +%m`
dd=`date +%d`

echo "Today is : $yy $mm $dd"


#!/usr/bin/ksh

date '+%m %d %Y' | 
{ 
read m d Y
d=`expr "$d" + 1` 
case "$d" in 
        0) 
           m=`expr "$m" + 1` 
                case "$m" in 
                        0) 
                           m=12 
                           Y=`expr "$Y" + 1` 
                        ;; 
                esac 
        d=`cal $m $Y | grep . | fmt +1 | tail +1` 
esac 

echo "Tomorrow is : $m $d $Y" 



d=`expr "$d" + 1` 
case "$d" in 
        0) 
           M=`expr "$m" + 1` 
                case "$m" in 
                        0) 
                           m=12 
                           Y=`expr "$Y" + 1` 
                        ;; 
                esac 
        d=`cal $m $Y | grep . | fmt +1 | tail +1` 
esac 

echo "The day after tomorrow is : $Y$m$d"



d=`expr "$d" + 1` 
case "$d" in 
        0) 
           M=`expr "$m" + 1` 
                case "$m" in 
                        0) 
                           m=12 
                           Y=`expr "$Y" + 1` 
                        ;; 
                esac 
        d=`cal $m $Y | grep . | fmt +1 | tail +1` 
esac 

echo "Two days from today is : $Y$m$d"


}
exit


Last edited by libertyforall; 07-31-2010 at 12:24 AM..
Sponsored Links
    #2  
Old 07-30-2010
methyl methyl is offline Forum Staff  
Moderator
 

Join Date: Mar 2008
Posts: 5,652
Thanks: 205
Thanked 560 Times in 539 Posts
The code is clearly a bad adaptation of a clever script to calculate yesterday's date.
It basically it would work for yesterday's date with:

Code:
d=`expr "$d" - 1`
m=`expr "$m" - 1` 
Y=`expr "$Y" - 1`

But it does not work for tomorrow's date with:

Code:
d=`expr "$d" + 1`
m=`expr "$m" + 1` 
Y=`expr "$Y" + 1`

Btw. I get a syntax error with "fmt -1" .
What Operating System and Shell are you using?

There may be better ways to do this depending on what Operating System and Shell you have.

As it stands the script is repairable by changing the logic to suite future dates.
The script contains a method for finding the last day of the month from the output of unix "cal". You'd need to determine whether the day in question is the last day of this month before deciding how to increment day month and year.
Sponsored Links
    #3  
Old 07-30-2010
Registered User
 

Join Date: Feb 2010
Location: Hawaii
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Ok. How exactly would I do that? I am using a C shell. Is there a way to read tomorrows date or have it print on the screen with perhaps the cal function? I could possibly use the cut command print the day. If not, how would I "change the logic to suite future dates?"

---------- Post updated at 07:44 AM ---------- Previous update was at 07:42 AM ----------

I am actually running a program that runs out 60 hours so I would need to be able to write the dates for as long as 2 days after tomorrow.

---------- Post updated at 08:34 AM ---------- Previous update was at 07:44 AM ----------

Would it be possible to use the +1 with a -j command to get the number of the day of year then convert the modified julian date back into the standard date?

---------- Post updated at 09:21 AM ---------- Previous update was at 08:34 AM ----------

Ok I found a pretty simple way to do it but I still need to have the month written numerically. How could I do it in conjuction with this command?
Code:
date -d "+2 day"



---------- Post updated at 09:30 AM ---------- Previous update was at 09:21 AM ----------

I figured it out! I can simply do this
Code:
date -d "+3 day" +%y%m%d

    #4  
Old 08-02-2010
methyl methyl is offline Forum Staff  
Moderator
 

Join Date: Mar 2008
Posts: 5,652
Thanks: 205
Thanked 560 Times in 539 Posts
I'm sure that in your next post you will mention what Operating System you have and what Shell you are using.
The script posted in post #1 was clearly not "C shell".
The extended "date" functions are not available in standard unix, so we guess that you are running some sort of Linux.
Sponsored Links
    #5  
Old 08-02-2010
fpmurphy's Avatar
who?
 

Join Date: Dec 2003
Location: /dev/ph
Posts: 4,043
Thanks: 35
Thanked 282 Times in 263 Posts
Looks like the Korn shell
Quote:
#!/usr/bin/ksh


---------- Post updated at 01:06 PM ---------- Previous update was at 12:45 PM ----------

If on Linux, ksh is probably actually ksh93. In that case you can do

Code:
$ printf "%T\n"  today
Mon Aug  2 00:00:00 EDT 2010
$ printf "%T\n"  "today + 1 day"
Tue Aug  3 00:00:00 EDT 2010
$ printf "%T\n"  "today + 2 day"
Wed Aug  4 00:00:00 EDT 2010
$

Sponsored Links
    #6  
Old 08-11-2010
Registered User
 

Join Date: Feb 2010
Location: Hawaii
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
I am running on a Linux Box.

[rhuff@huina working]$ echo $SHELL
/bin/csh

---------- Post updated at 12:01 PM ---------- Previous update was at 11:56 AM ----------

My script worked for the date. the kshell script was partially borrowed script from an earlier project which I have had to modify. The line
Code:
#!/usr/bin/ksh

is a dead line. In any case, I have a running Cronjob twice daily. I want to add a line to the script that searches for a file. If the file does not exist, I need to kill the job and run it again in one hour. I am running the program and 2 am and 2pm. I would want the job to run again at 3am and 3pm respectively. How could I do this and still keep the cronjob running twice daily if I killed it once?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 08:11 AM
Typing the @ sign creates new line. ricnetman HP-UX 3 04-09-2010 12:00 PM
naming files that csplit creates juliette salexa UNIX for Dummies Questions & Answers 8 08-24-2009 12:04 PM
HP-Unix; .net application creates processes NicoMan HP-UX 12 02-25-2009 12:50 PM
SCO 5.0.7 Cron creates files with 600, need 644 65bit UNIX for Dummies Questions & Answers 2 02-03-2009 12:01 PM



All times are GMT -4. The time now is 03:56 AM.