Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sheel script generating all weekday of previous month

Posted on 2006-07-14
7
Medium Priority
?
313 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
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 400 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

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!

Question has a verified solution.

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

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 …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

783 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