Solved

Sheel script generating all weekday of previous month

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
Define unique primary key 9 89
sumDigits challenge 9 127
post4 challenge 28 93
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

22 Experts available now in Live!

Get 1:1 Help Now