Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Finding days in previous month Post 302686277 by Don Cragun on Tuesday 14th of August 2012 08:18:36 AM
Old 08-14-2012
I took your original posting, reformatted it so I could follow what was going on, fixed the syntax errors, and fixed some logic bugs. There are comments in-line showing what I changed and why.
Code:
#!/bin/ksh
# indentation added to make it easier to see the logic
day=1 # not needed, it is always set below to the last day of the previous month
# delete this duplicate line: #!/bin/ksh
# delete this duplicate line: day=1
month=1
year=2012
if [ $day -eq 1 ] # added a missing <semicolon> or <newline> here
then { # [ change to {
        if [[ $month -eq 1 || $month -eq 2 || $month -eq 4 || $month -eq 6 || $month -eq 8 || $month -eq 9 || $month -eq 11 ]]
        # <space> added after [ and before ] on above line
        # and [ and ] changed to [[ and ]], respectively, and
        # 31 day months changed from 12, 4, 6, 8, & 10 to 12, 1, 3, 5, 7, 8, & 10
        then prevmonth=31
        elif [[ $month -eq 5 || $month -eq 7 || $month -eq 10 || $month -eq 12 ]]
        # <space> added after [ and before ] on above line
        # and [ and ] changed to [[ and ]], respectively, and
        # 30 day months changed from 1, 3, 5, 7, 9, & 11 to 4, 6, 9, & 11
        then prevmonth=30
        elif [ $month -eq 3 ]  # this line could just be else
        # <space> added after [ and before ] on above line
        then {  if [[ $((year%100)) -eq 0 && $((year%400)) -eq 0 ]]
                # <space> added after [ and before ] on above line
                # and [ and ] changed to [[ and ]], respectively
                # and $((...)) added around both year%... calculations
                # and then [ changed to then {
                then prevmonth=29
                elif [[ $((year%100)) -ne 0 && $((year%4)) -eq 0 ]]
                # <space> added after [ and before ] on above line
                # and [ and ] changed to [[ and ]], respectively
                # and $((...)) added around both year%... calculations
                then prevmonth=29
                else prevmonth=28
                fi
        } # ] changed to }
        fi #this was missing
} # ] changed to }
fi #this was missing
if [[ $day -eq 1 && $month -eq 1 ]] # <space> added after if
# <space> added after [ and before ] on above line
# and [ and ] changed to [[ and ]], respectively
then    year=$(($year-1))
        # $(...) change to $((...))
fi
echo $prevmonth

I hope this helps.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

file name using previous month

This has probably been asked 100 times, but I couldn't find any articles on point. I have a script that runs on the last day of every month at 11:30pm. If cats a number of input tables that were created the previous month (or earlier), combines them into one master file and erases the indivual... (1 Reply)
Discussion started by: beilstwh
1 Replies

2. Shell Programming and Scripting

How to get previous month files

Hi, My task to to delete files which are of previous months. I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files i.e in Dec i have delete all nov files which look like 200511DD.XLS in Jan i have to delete all Dec files 200512DD.xls ... (7 Replies)
Discussion started by: savitha
7 Replies

3. Shell Programming and Scripting

Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008). Here is what I have so far. #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if then ... (11 Replies)
Discussion started by: sirrtuan
11 Replies

4. UNIX for Dummies Questions & Answers

How to get the previous month by using `date`

Hello, I'm new to shell scripting. We've develop a script which will grep a file on the search criteria, MON (Jan/Feb/Mar/etc). But we should set this sript in cron which will run on every first day of the month. The problem I'm having is, when I run the script, it is displaying the contents of... (7 Replies)
Discussion started by: suneelj
7 Replies

5. Shell Programming and Scripting

Function to get previous month

Can someone help me creating a function which will give me previous months. like for example if the date is 200902 and if i call my function and pass a parameter 2 i want to get 200812 as the answer. or if i pass 200902 with a parameter 7 then my function should give me the date as 200807. (1 Reply)
Discussion started by: Lincy
1 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Number of days in the previous month

Hi all. I am scripting in a POSIX shell on HPUX. I am running a script that needs to determine the number of days in a month. I found this on the forum and it works great: X=`cal $(date +%m) $(date +%Y) | grep -v '' | wc -w` The issue is that I am running the script on the 7th day of... (11 Replies)
Discussion started by: lyoncc
11 Replies

8. UNIX for Dummies Questions & Answers

Get the previous month from date

Hi All, I am using the below code to get the year and month from date: Below gives output like 201212. dt=`date '+%Y%m'` how do i get the previous month value(ie: subtract 1 from date) example output: dt=201211 Please help. :confused: (3 Replies)
Discussion started by: abhi_123
3 Replies

9. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies
CAL(1)							    BSD General Commands Manual 						    CAL(1)

NAME
cal -- displays a calendar SYNOPSIS
cal [-smjy13] [[month] year] DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the current month is displayed. The options are as follows: -1 Display single month output. (This is the default.) -3 Display prev/current/next month output. -s Display Sunday as the first day of the week. (This is the default.) -m Display Monday as the first day of the week. -j Display Julian dates (days one-based, numbered from January 1). -y Display a calendar for the current year. A single parameter specifies the year (1 - 9999) to be displayed; note the year must be fully specified: ``cal 89'' will not display a calen- dar for 1989. Two parameters denote the month (1 - 12) and year. If no parameters are specified, the current month's calendar is displayed. A year starts on Jan 1. The Gregorian Reformation is assumed to have occurred in 1752 on the 3rd of September. By this time, most countries had recognized the ref- ormation (although a few did not recognize it until the early 1900's.) Ten days following that date were eliminated by the reformation, so the calendar for that month is a bit unusual. HISTORY
A cal command appeared in Version 6 AT&T UNIX. OTHER VERSIONS
Several much more elaborate versions of this program exist, with support for colors, holidays, birthdays, reminders and appointments, etc. For example, try the cal from http://home.sprynet.com/~cbagwell/projects.html or GNU gcal. BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 08:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy