Solved

Sheel script generating all weekday of previous month

Posted on 2006-07-14
7
302 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
7 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

12 Experts available now in Live!

Get 1:1 Help Now