Getting the previous month as input for the script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the previous month as input for the script
# 1  
Old 03-12-2014
Getting the previous month as input for the script

Hi Experts ,

I have scheduled a script "createstructure "in crontab. The script runs every month on 25th .
I am trying to pass the input to the script as the <year><previousmonth>.For example ,if it is running in the month of March 2014 ,the input should be "201402" (2014-year 02-previous month ).For Jan 2015 , the input should be 201412.
 ./createstructure 201402 -->if running in the month of march.

Running Red Hat Enterprise in the server.

Is it doable ?


Last edited by vbe; 03-12-2014 at 12:15 PM.. Reason: code tags please!
# 2  
Old 03-12-2014
dt=$(echo "$(date --date="1 month ago" +%Y%m)")
./createstructure $(echo $dt)

Last edited by in2nix4life; 03-12-2014 at 12:22 PM..
This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 03-12-2014
Great..Thanks a lot in2nix4life..

It would be helpful for me if you could just split and explain the command.

Thanks again..Smilie
# 4  
Old 03-12-2014
Originally Posted by in2nix4life
dt=$(echo "$(date --date="1 month ago" +%Y%m)")
./createstructure $(echo $dt)

That's an awful lot of useless echoes... You can boil this whole thing down to

./createstructure "$(date --date="1 month ago" +%Y%m)"

# 5  
Old 03-12-2014
dt=$(date --date="1 month ago" +%Y%m) is sufficient
       -d, --date=STRING
              display time described by STRING, not `now'

So, 1 month ago tells date command to fetch the date that is 1 month ago in the format %Y%m (%Y --> 2014; %m --> 02)

Also, you could say "-1 month" -> minus has the same effect as ago
# 6  
Old 03-12-2014
But on executing this "$(date --date="1 month ago" +%Y%m)" it is giving

$ "$(date --date="1 month ago" +%Y%m)"
-bash: 201402: command not found

# 7  
Old 03-12-2014
In KSH93:

LM=$( printf "%(%Y%m)T" "last month" )

./createstructure "$LM"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the previous month files using shell script?

could you please assist the below query. i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied. Please find the below directory structure:- ls -lrt total 1824... (2 Replies)
Discussion started by: venkat918
2 Replies

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

6. 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

7. 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

8. 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

9. 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
Login or Register to Ask a Question