Solved

Sheel script generating all weekday of previous month

Posted on 2006-07-14
7
310 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 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