file name using previous month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file name using previous month
# 1  
Old 08-04-2004
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 files. It also compresses any master files from previous months. My problem is that if the script is run on August 31, it will take all the files from July, and make a master file with the name EKVI0804.DAT

What I want to do is a have it use the previous month (EKVI0704.DAT) for the file (since the files belong to July). Below is my current script. Any help would be appreciated.

==========================
#!/bin/ksh

# Because a cron job can not be told to run on the last day of a month,
# it will be run on the 28-31 of every month at 11:30pm. The following
# code will determine if we are on the last day of the month. Because a
# cron job doesn't have any environmental variables available from the
# submitting session, all paths need to be hard coded. If this job needs
# to be run for a gers production except live, please make the
# approporate modifications to the path names. The test will be for any
# file that was created before the current month.

days_in_month=$(echo $(cal) | awk '{print $NF}')
day_of_month=$(date +%d)
if [[ $days_in_month -eq $day_of_month ]]
then
touch -m -t `date +%Y%m`010000.01 /gers/live/adhoc/ekvidatetestfile
cd /gers/live/datafiles
find . \( ! -name . -prune \) -name "EKVI*\.DAT" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec compress {} \;
cd /gers/live/adhoc
find . \( ! -name . -prune \) -name "EKVI*" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec cat {} > /gers/live/datafiles/EKVI`date +%m%y`.DAT \;
find . \( ! -name . -prune \) -name "EKVI*" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec rm {} \;
rm /gers/live/adhoc/ekvidatetestfile
else
echo not the last day of the month
exit
fi
# 2  
Old 08-04-2004
I figured out how to do it. It might not be eligent, but it works.

===============================
#!/bin/ksh

# Because a cron job can not be told to run on the last day of a month,
# it will be run on the 28-31 of every month at 11:30pm. The following
# code will determine if we are on the last day of the month. Because a
# cron job doesn't have any environmental variables available from the
# submitting session, all paths need to be hard coded. If this job needs
# to be run for a gers production except live, please make the
# approporate modifications to the patch names. The test will be for any
# file that was created before the current month.

days_in_month=$(echo $(cal) | awk '{print $NF}')
day_of_month=$(date +%d)

last_month=$(expr `date +%m` - 1)
last_year=$(date +%y)
if [[ $last_month -eq 0 ]]
then
last_month=12
last_year=$(expr $last_year - 1)
fi
if [[ $last_month -lt 10 ]]
then
last_month="0"$last_month
fi


file_name=$last_month$last_year

if [[ $days_in_month -eq $day_of_month ]]
then
touch -m -t `date +%Y%m`010001.01 /gers/live/adhoc/ekvidatetestfile
cd /gers/live/datafiles
find . \( ! -name . -prune \) -name "EKVI*\.DAT" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec compress {} \;
cd /gers/live/adhoc
find . \( ! -name . -prune \) -name "EKVI*" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec cat {} > /gers/live/datafiles/EKVI$file_name.DAT \;
find . \( ! -name . -prune \) -name "EKVI*" -type f ! -newer /gers/live/adhoc/ekvidatetestfile -exec rm {} \;
rm /gers/live/adhoc/ekvidatetestfile
else
echo not the last day of the month
exit
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

6. Shell Programming and Scripting

Display month for Previous day

Hello - I have one question regarding the date. I wanted to display the month name for previous day. The output should be as follows... 5-Feb-09 => February 1-Feb-09 => January 28-Feb-09=> February Here is the code i am using to get the output.... date '+%m %d %Y' | { read MONTH DAY... (4 Replies)
Discussion started by: govindts
4 Replies

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

8. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

9. Shell Programming and Scripting

How to get previous month files

Hi, My task to to delete files which are of previous months. I have files named as follows *CCYYMMDD.xls. on a particular day i have delete previous months files i.e in Dec i have delete all nov files which look like 200511DD.XLS in Jan i have to delete all Dec files 200512DD.xls ... (7 Replies)
Discussion started by: savitha
7 Replies
Login or Register to Ask a Question