How to pass current year and month in FOR LOOP in UNIX shell scripting? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to pass current year and month in FOR LOOP in UNIX shell scripting?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-17-2012
shoan shoan is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 17 December 2012, 4:43 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to pass current year and month in FOR LOOP in UNIX shell scripting?

Hi Team,

I have created a script and using FOR LOOP like this and it is working fine.


Code:
for Month in  201212 201301 201302 201303  
do

echo "Starting the statistics gathering of $Month partitions "  

done

But in my scripts the " Month " variable is hard-coded. Can you please any one help me how to capture the month variable in UNIX in this format ("YYYYMM") and pass it dynamically in the FOR LOOP.

Thanks in Advance
Shoan
Sponsored Links
    #2  
Old 12-17-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 December 2014, 4:49 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts
If your date command supports -d option, then try:-

Code:
#!/bin/bash

DT="20121201"

for count in {1..5}
do
        echo $DT | cut -c 1-6
        DT=$( date -d"$DT +1 month" +"%Y%m%d" )
done

Sponsored Links
    #3  
Old 12-17-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 16 December 2014, 10:34 AM EST
Location: India
Posts: 1,837
Thanks: 25
Thanked 527 Times in 506 Posts
Quote:
Originally Posted by shoan View Post
Can you please any one help me how to capture the month variable in UNIX in this format ("YYYYMM") and pass it dynamically in the FOR LOOP.
Capture from where?
    #4  
Old 12-17-2012
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 December 2014, 10:06 PM EST
Posts: 3,051
Thanks: 114
Thanked 983 Times in 920 Posts
You could init month and year vars and increment in a loop like this:


Code:
YEAR=`date +%Y`
MONTH=`date +%m`
for i in 1 2 3 4
do
   printf "Starting the statistics gathering of %d%02d partitions \n" $YEAR $MONTH
   let MONTH=10#$MONTH+1
   if [ $MONTH -gt 12 ]
   then
      let YEAR=YEAR+1
      let MONTH=1
   fi
done

Note the 10# above is required for bash, as leading zeros in Aug and Sep cause bash some issues as it tries to convert to octal.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix man command to find out month of the year? janetroop95 UNIX for Dummies Questions & Answers 2 08-30-2012 01:28 PM
[Solved] Shell script for converting the day of the year to day/month/year StudentFitz Shell Programming and Scripting 10 11-04-2011 01:16 PM
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 09:11 AM
Convert unix timestamp to year month day format ? vilius AIX 1 01-06-2010 03:30 AM
how to get the last month and year in UNIX Vijay06 UNIX for Advanced & Expert Users 2 08-18-2007 09:25 AM



All times are GMT -4. The time now is 11:59 PM.