Solved

C-Shell

Posted on 1997-11-19
13
300 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:dkingsf
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1295629
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
 
LVL 1

Expert Comment

by:dhughes111797
ID: 1295630
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
 

Author Comment

by:dkingsf
ID: 1295631
Edited text of question
0
 

Author Comment

by:dkingsf
ID: 1295632
Adjusted points to 125
0
 
LVL 1

Expert Comment

by:dhughes111797
ID: 1295633
Why not run it out of cron instead? That way you can use
the date stuff that ozo and I showed.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1295634
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Expert Comment

by:dhughes111797
ID: 1295635
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
 

Author Comment

by:dkingsf
ID: 1295636
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
 
LVL 84

Expert Comment

by:ozo
ID: 1295637
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
 

Author Comment

by:dkingsf
ID: 1295638
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
 
LVL 84

Expert Comment

by:ozo
ID: 1295639
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
 
LVL 84

Expert Comment

by:ozo
ID: 1295640
#!/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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 250 total points
ID: 1295641
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now