?
Solved

C-Shell

Posted on 1997-11-19
13
Medium Priority
?
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 750 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

718 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