|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Capture from where?
|
|
#4
|
||||
|
||||
|
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
doneNote 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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|