How to calculate months and display in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate months and display in shell scripting
# 1  
Old 05-26-2011
How to calculate months and display in shell scripting

I just want to know, how do we calculate the months in shell scripting.

If i give the input as 20-01-2011, the output should be 20-02-2011, 20-03-2011 or 20-04-2011........

How do i get this ?

Cheers.
# 2  
Old 05-26-2011
not sure exactly what you mean...do you want all months for a given year ? or all successive months, or just next month?

here's a start point for you to have a play with:

Code:
#  for x in {1..12}; do printf "20-%02d-2011\n" $x ;done
20-01-2011
20-02-2011
20-03-2011
20-04-2011
20-05-2011
20-06-2011
20-07-2011
20-08-2011
20-09-2011
20-10-2011
20-11-2011
20-12-2011

HTH
# 3  
Old 05-26-2011
ksh93 has date arithmetic.
# 4  
Old 05-27-2011
Hi Tytalus,

Thanks for your time....

What I need is, for example I am providing the input as 01-01-2011.

The system should accept it and increment it by one month and should display the output as 01-02-2011.

Similarly if the input is 01-12-2011, the output should be 01-01-2012.

Any solution for this will be of great help.....

Cheers.
# 5  
Old 05-27-2011
Your solution is not so simple as to add one to the month and to handle the year.

What if your input is: 01-31-2011

What is your expected output?
# 6  
Old 05-27-2011
Code:
ms:/home/unxsa/functions>today
27May2011
ms:/home/unxsa/functions>today -p 30
26Jun2011
ms:/home/unxsa/functions>cat today
# Name: today
#
# Purpose: Prints today's date, plus or minus the given number of days.
#          Default is today's date.
#
# Date: 2006/06/08
#
# Version: 1.0
#
# Required Parameters:
# -------------------------------------------------------------------------
#
# Optional Parameters:
#   -f [format]- format of date to use
#   -p #- number of days to add
#   -m #- number of days to subtract
#   -c c- character to use in the date string
#   -s- use short year string
# -------------------------------------------------------------------------
#
# Change History:
#
# Date          Name            Comments
# _________________________________________________________________________
# 2006/06/08    selbyp          Created.
# 21 jan 2011   purdym          converted to ksh93
#
#
#
#
#
#
#
#
#
#
#
#



###################################################################################
# function
###################################################################################
function today {

   USAGE=":f:p:m:c:sh"

   FORMAT=0
   PLUS=0
   MINUS=0
   SEPCHAR=""
   SHORT=0

   if [[ $# -gt 0 ]]
   then
      while getopts "${USAGE}" OPT
      do
         case ${OPT} in
            f) FORMAT=${OPTARG};;
            p) PLUS=${OPTARG};;
            m) MINUS=${OPTARG};;
            c) SEPCHAR=${OPTARG};;
            s) SHORT=1;;
            h) cat <<EOF
Syntax:  today { [-p n] OR [-m n] } [-f FORMAT] [-c CHAR] [-s] [-h]
Parameters:
[-p n]-Print today's date, plus n days
[-m n]-Print today's date, minus n days
[-f FMT]-Print date using specified format (DMY/MDY/YMD), default looks like Jun092006
[-c c]-Separate dates using the character c
[-s]-Use a short year in the date
[-h]-View this help
Default:  Print today's date in the default format.
EOF
            return 0
            ;;
            ?) echo "today(): unrecognized option: ${OPT}"
            return 1;;
         esac
      done

      shift $((${OPTIND} - 1))
      ARGUMENTS=$@

      if (( ${PLUS} != 0 && ${MINUS} != 0 ))
      then
         RETCODE=1
         (( ${DEBUG} != 0 )) && print "today(): Attempted to add and subtract simultaneously."
         return $RETCODE
      fi

      if [[ ${FORMAT} -ne 0 && ${FORMAT} != "YMD" && ${FORMAT} != "MDY" && ${FORMAT} != "DMY" ]]
      then
         RETCODE=2
         (( ${DEBUG} != 0 )) && print "today(): Unrecognized format: ${FORMAT}"
         return $RETCODE
      fi

      if (( ${#SEPCHAR} > 1 ))
      then
         RETCODE=3
         (( ${DEBUG} -ne 0 )) && print "today(): Bad separating character: ${SEPCHAR}"
         return $RETCODE
      fi
   fi

   eval `date "+DAY=%d; MONTH=%m; YEAR=%Y`

   if (( $PLUS > 0 ))
   then
      DAY=$((DAY + PLUS))

   elif (( $MINUS > 0 ))
   then
      DAY=$((DAY - MINUS))

   fi

   if (( $DAY <= 0 ))
   then
      while (( $DAY <= 0 ))
      do
         MONTH=$((MONTH - 1))
         if (( $MONTH == 0 ))
         then
            YEAR=$(( YEAR - 1))
            MONTH=12
         fi
         set -A DAYS $(cal $MONTH $YEAR)
         XDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
         DAY=$((XDAY + DAY))

      done
   else
      set -A DAYS $(cal $MONTH $YEAR)
      MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}

       if (( $DAY > $MAXDAY ))
       then
          while (( $DAY > $MAXDAY ))
          do
             DAY=$((DAY - MAXDAY))
             MONTH=$((MONTH + 1))
             if (( MONTH > 12 ))
             then
                YEAR=$((YEAR + 1))
                MONTH=1
             fi
             set -A DAYS $(cal $MONTH $YEAR)
             MAXDAY=${DAYS[$((${#DAYS[*]} - 1 ))]}
             done
          fi
       fi

       if (( $SHORT != 0 ))
       then
          if (( ${#YEAR} == 4 ))
          then
             OLD_YEAR=${YEAR}
             typeset -Z2 YEAR
             YEAR=$( echo ${OLD_YEAR} | cut -c 3-4 )

          elif (( ${#YEAR} != 2 ))
          then
             if [[ ! -z $DEBUG ]] && [[ $DEBUG -gt 0 ]]
             then
                echo "today(): what kind of year is this: ${YEAR}??"
                return 1
             fi
          fi
       fi

    typeset -Z2 F_DAY F_MONTH
    F_DAY=$DAY
    F_MONTH=$MONTH

       if [[ $FORMAT == "YMD" ]]
       then
          print "${YEAR}${SEPCHAR}${F_MONTH}${SEPCHAR}${F_DAY}"

       elif [[ $FORMAT == "DMY" ]]
       then
          print "${F_DAY}${SEPCHAR}${F_MONTH}${SEPCHAR}${YEAR}"

       elif [[ $FORMAT == "MDY" ]]
       then
          print "${F_MONTH}${SEPCHAR}${F_DAY}${SEPCHAR}${YEAR}"

       else
          case $F_MONTH in
             01) MONTH_STR=Jan;;
             02) MONTH_STR=Feb;;
             03) MONTH_STR=Mar;;
             04) MONTH_STR=Apr;;
             05) MONTH_STR=May;;
             06) MONTH_STR=Jun;;
             07) MONTH_STR=Jul;;
             08) MONTH_STR=Aug;;
             09) MONTH_STR=Sep;;
             10) MONTH_STR=Oct;;
             11) MONTH_STR=Nov;;
             12) MONTH_STR=Dec;;
          esac
       print ${F_DAY}${MONTH_STR}${YEAR}
    fi

}

# 7  
Old 05-27-2011
Code:
read DATE; date -d "$DATE 1 month"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to display only the first 5 running process using top in shell scripting?

topfunc() { top } topfunc Here i used the top command inside a function,and i called the function. when executing this bash file i get all the process which are using by the kernel i just want to display only the first 5 running process. is it possible? (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

2. Shell Programming and Scripting

How to calculate average of csv using shell scripting?

Hi, I need to calculate the average of the following values using shell scripitng. Can anyone please suggest a solution? ... (10 Replies)
Discussion started by: karan pratap si
10 Replies

3. Shell Programming and Scripting

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

4. Shell Programming and Scripting

Need help in shell Scripting to display a output from a command

Please find my below requirement and see if you can help me on this. I am looking for a shell script which can provide me the below output. Manuall steps which i am doing now 1) First I source the File $ . ./WC_env.sh 2) Execute the command $ /app/oracle/product/mos/bin/mosotl -url... (2 Replies)
Discussion started by: sudheshpn@gmail
2 Replies

5. UNIX for Dummies Questions & Answers

Cal command to display 5 months ?

Hi, I'm curious to know if we can display 5 months of a calendar using the cal command. I know we can three successive months (cal -3) but I wanted to know if we can do it with 5 months for example. (Give a specific month, and get as a result two previous months + the month in question + two... (12 Replies)
Discussion started by: Imane
12 Replies

6. Shell Programming and Scripting

Display the First and Last name from a file using shell scripting

I am new to shell scripting and doing a similar thing @ work Stan:Smith:Detroit:MI Jim:Jones:Farmington Hills:MI Jack:Frost:Denver:CO Sue:Apple:New York:NY Cindy:Thompson:Battle Creek:MI John:Smith:Denver:CO George:Jones:New York:NY Need to create a shell script This script will display... (1 Reply)
Discussion started by: jakemathew
1 Replies

7. Programming

C++ Display months of year in a double dimensional array

stupid question (0 Replies)
Discussion started by: puttster
0 Replies

8. Shell Programming and Scripting

calculate 13 months ago

hi, I have a big file that contains datas since 4 years ago. I need re-create this file but just lines that are 13 months ago from today. see what I have: ( I have a file.ksh that calls this file.scl ok !!) ======== file.scl ================ /STATISTICS=stderr /STABLE /NODUPLICATES... (4 Replies)
Discussion started by: andrea_mussap
4 Replies

9. Shell Programming and Scripting

calculate 13 months ago

hi, I have a big file that contains datas since 4 years ago. I need re-create this file but just lines that are 13 months ago from today. see what I have: ( I have a file.ksh that calls this file.scl ok !!) ======== file.scl ================ /STATISTICS=stderr /STABLE /NODUPLICATES... (3 Replies)
Discussion started by: andrea_mussap
3 Replies

10. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question