Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-17-2012
Registered User
 
Join Date: Mar 2012
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
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,296
Thanks: 153
Thanked 733 Times in 705 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
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,565
Thanks: 14
Thanked 440 Times in 425 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
Registered User
 
Join Date: Oct 2010
Posts: 2,106
Thanks: 65
Thanked 610 Times in 588 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
Reply

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 12: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 12:16 PM
print previous month (current month minus 1) with Solaris date and ksh slashdotweenie UNIX for Dummies Questions & Answers 7 05-14-2010 08:11 AM
Convert unix timestamp to year month day format ? vilius AIX 1 01-06-2010 02:30 AM
how to get the last month and year in UNIX Vijay06 UNIX for Advanced & Expert Users 2 08-18-2007 08:25 AM



All times are GMT -4. The time now is 09:48 PM.