Solved

Sheel script generating all weekday of previous month

Posted on 2006-07-14
7
308 Views
Last Modified: 2013-12-26
I want to generate all weekday of previous month and output it to a text file using shell script.

For example, current month is July 2006 . after running the script, it will output a file called "date.txt" which contains :
01-06-2006
02-06-2006
05-06-2006
07-06-2006
.......
.......
30-06-2006

Pls help.
0
Comment
Question by:mikekwok
[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
  • 3
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 100 total points
ID: 17112001
Try this:

1. Shell script:
#!/bin/sh
MON=`date '+%m'`
YEAR=`date '+%Y'`
if [ $MON -eq 1 ]
then
   MON=12
   YEAR=`expr $YEAR - 1`
else
   MON=`expr $MON - 1`
fi
cal $MON $YEAR | grep -v "[A-Z]" | cut -c4-17 | awk -f formatit.awk - $MON $YEAR 2>/dev/null

2. Awk script (formatit.awk):
BEGIN {
      MON=ARGV[2]
      YEAR=ARGV[3]
}
{
      for (i=1; i<=NF; i++)
            print $i "-" MON "-" YEAR;
}
0
 

Author Comment

by:mikekwok
ID: 17174458
I name the shell script as "gen_weekday.sh", I put this in crontab but the weekday file cannot generate successfully.
58 17 25 07 * /aaa/home/scripts/gen_weekday.sh > /aaa/home/scripts/oli_valo_all_date.txt 2>&1

The weekday file is as follow:

awk: can't open 6
 record number 6
1--
2--
5--
6--
7--
8--
9--
12--
13--
14--
15--
16--
19--
20--
21--
22--
23--
26--
27--
28--
29--
30--


Please help. Thanks,
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17178354
Where did you put the awk script, and do you refer to it by its absolute path in gen_weekday.sh? Just to be certain that there's no problem with the version of cal on your path, what do you get when you run the command:
       cal 7 2006

Does the output look exactly like this?

     July 2006
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

(note that this displays with a proportional font - the "1" should align under Sa with a fixed-width font.)
0
 

Author Comment

by:mikekwok
ID: 17401554
Hello bpmurray,

Sorry for the late reply.The output look exactly like this. But when I try to schedule it to run in the gen_weekday.sh in the crontab. The file cannot be generated. the output is what I mentioned in 07/25/2006 03:02AM PDT. Do you have any suggestion to fix the problem?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17403217
Just looked again at this, and the error message is an indication as to the problem - "awk: can't open 6". It looks like you're missing a "-" in the call to awk:
    awk -f formatit.awk - $MON $YEAR

There should be a "-" surrounded by spaces, between ".awk" and "$MON". Can you confirm that this is the case?
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 85
Strange date stored 22 171
Importing Special Characters in Dynamics GP Through Integration Manager 3 89
Work with App store 7 91
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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