Solved

C-Shell

Posted on 1997-11-19
13
303 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
how do i create updater to My Activex application? 3 88
notReplace  challenge 53 131
viewing source code from eclipse 13 108
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

803 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