C-Shell

New to shell.  Need to be able to use the date function to create variables which are used to create filenames.  Program is run for 'n' days ( 1 to whatever ).  I need to be able to handle the change for the month/day ( 30 days hath September....blah blah).  i.e.  The script run for 30 days on the 1st of the month will work fine.  But run for 30 days on the 20th of the month increments the var to the 49th/50th day of the month.  I need a function or some idea of how to handle the month change (including leap years) without nesting a bunch of ifs.
Here's part of the script......
DIR=`date +%Y%m%d`
TODAY=`date +%m%d`
DATE=`date +%Y%m%d`
       for z in 1 2 3 4 5 6 7 8 9 10 11 12
    do
        touch /daily/$DIR/$TODAY
        echo '*****     '
        echo scan for $DATE
        DATE=`expr $DATE + 1`
        TODAY=`expr $TODAY + 1`
    done
dkingsfAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ahoffmannConnect With a Mentor Commented:
I suggest using  date +%j  instead of  date +%m%d%y

About you problem when
  "run for 30 days on the 20th of the month ..""
you need to explain how your script is doing this.
I.g. the solution looks like:

#! /bin/csh -f
@ today=`date +%j`
while(1)
  @ new=`date +%j`
  if ($new != $today) then
     @ today=$new
  endif
  if ($today < 367) then
     # need more checks for leap years
     @ tomorrow=$today + 1
  else
     @ tomorrow=1
  endif
  #
  # your code
  #
  sleep 43200
end
0
 
ozoCommented:
I'm not sure I understand your question.
Do you want to do something like this?

#!/bin/csh
set d = `date`
echo $d > file.$d[3]

0
 
dhughes111797Commented:
The easiest way to do this is just to do this in your script
each day.

set file=myfile.`date '+%m%d%y'`
(today, it gives myfile.111997)
You can use whatever combination of % options you want. See
the date man page for more details. Really avoid doing this
yourself. The system does it much better.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
dkingsfAuthor Commented:
Edited text of question
0
 
dkingsfAuthor Commented:
Adjusted points to 125
0
 
dhughes111797Commented:
Why not run it out of cron instead? That way you can use
the date stuff that ozo and I showed.

0
 
ozoCommented:
That looks more like an sh script than a csh script
but how about
 for z in 1 2 3 4 5 6 7 8 9 10 11 12
 do
 TZ=GMT-`expr $z \* 24`
 export TZ
 DATE=`date +%Y%m%d`
 done
0
 
dhughes111797Commented:
Well, that sort of depends on the OS. Solaris2.5 for instance,
won't deal with TZ > 24 for instance and just does TZ-24 so
it may work for a few days, but not for the whole range like
he wants. Of course, that's OS dependent..
0
 
dkingsfAuthor Commented:
Thanks for the reply.  Looks like you're trying to help, but like I said, I'm pretty new to shell programming.  I guess I still don't get it.  I don't understand the expression  TX=GMT-`expr $z\*24`, except it has something to do with 24 hour time period? How will this help with the switch on the month issue?
0
 
ozoCommented:
DATE=`perl -e '($d,$m,$y)=(localtime time+24*3600*shift)[3,4,5]; printf "%d%02d%02d",$y+1900,$m+1,$d' $z`
0
 
dkingsfAuthor Commented:
Now you're giving me perl?  Hell, way out of my meager league.  If shell (C,Bourne,Korn) isn't going to work and I shoud go to perl or C++ then probably someone else more experienced should look into this.  I don't have time to learn everything.  But you can have the points for trying....thanks.
0
 
ozoCommented:
csh or sh will work, but may require either OS dependant TimeZone
settings or a bunch of expressions I thought you wanted to avoid.
It's probably easier in perl but if you want csh or sh, we could do that too...

0
 
ozoCommented:
#!/bin/sh
dy=`date +%j`
yy=`date +%Y`
y1=`expr $yy - 1`
dd=`expr $dy + $y1 \* 365 + $y1 / 4 - $y1 / 100 + $y1 / 400`
for z in 1 2 3 4 5 6 7 8 9 10 11 12
do
d=`expr $dd + $z`
y1=`expr $d \* 400 / 146097`
y=`expr $y1 + 1`
d=`expr $d - $y1 \* 365 - $y1 / 4 + $y1 / 100 - $y1 / 400`
l=`expr \( $y % 4 \| $y % 100 = 0 \& $y % 400 \) = 0`
d=`expr $d + \( $d \> 59 + $l \) \* \( 2 - $l \)`
m=`expr \( $d + 183 \) \* 12 / 367`
d=`expr $d + 183 - $m \* 367 / 12`
m=`expr \( $m + 6 \) % 12 + 1`
echo $y $m $d
done

0
All Courses

From novice to tech pro — start learning today.