nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

date manipulation

This is a discussion on date manipulation within the Shell scripting forums, part of the Development/Scripting category; Hi I am new to Unix. I am running Sun aix as an o/s. I have a requirement where need ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-04-2009, 09:24 PM
Junior Member
User
 
Join Date: Apr 2009
OS: Sun AIX
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
parmarjm is on a distinguished road
Default date manipulation

Hi
I am new to Unix.
I am running Sun aix as an o/s.

I have a requirement where need to create a file (.txt) using shell script.
In this file I have to pass /compute `date` in such a way that it gives
start dt = 032009
end = 032008

below is the script I have created which creates a desired file and populates start date as current date but dont know how to compute current date (minus one month) and end_dt as current date minus 12 months.

my script is as below:

Code:
#!/bin/ksh
rm stage3sg.txt
if [ ! -s stage3sg.txt ]
then
echo "Creating File"
cat << EOF > stage3sg.txt
echo start_dt `date "+%m%Y"`
EOF
fi
above script creates file stage3sg.txt.
When you check contents of stage3sg.txt is has
echo start_dt 042009 -- as output.

I need to get
start_dt 032009
end_dt 032008

Please guide me,
any help is appreciated.
Thanks
J

Last edited by nixcraft; 10-04-2009 at 02:48 AM.
Reply With Quote
  #2 (permalink)  
Old 09-04-2009, 01:12 PM
kasimani's Avatar
Senior Member
User
 
Join Date: Jul 2006
Location: India, Delhi
OS: CentOS, RedHat, Fedora, Ubuntu
Posts: 151
Thanks: 3
Thanked 1 Time in 1 Post
Rep Power: 4
kasimani is on a distinguished road
Send a message via Yahoo to kasimani
Default

use the below one

Code:
rm -f stage3sg.txt
if [ ! -s stage3sg.txt ]
then
echo "Creating File"
echo start_dt `date -d "1 month ago" "+%m%Y"` >> stage3sg.txt
year=`date -d "1 year ago " "+%Y"`
date=`date -d "1 day ago" "+%d"`
echo end_dt $date$year >> stage3sg.txt
echo Year=$year
echo DATE=$date
echo 
fi

Last edited by nixcraft; 10-04-2009 at 02:49 AM.
Reply With Quote
  #3 (permalink)  
Old 10-04-2009, 12:56 AM
Junior Member
User
 
Join Date: Apr 2009
OS: Sun AIX
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
parmarjm is on a distinguished road
Default

To Kashimani,

Hi Thanks for your response.
The script you have provieded didn't went thru.

here is my version of script works:

Code:
#!/bin/ksh
MONTH=`date +%m`
LAST_MONTH=$((`date +%m` - 1))

YEAR=`date +%Y`
LAST_YEAR=$((`date +%Y` -1))

if [ $LAST_MONTH -eq "0" ]
then
LAST_MONTH=12
YEAR=$LAST_YEAR
LAST_YEAR=$(($YEAR-1))
echo "If current month is January then last_month is $LAST_MONTH"
echo "and start_date year is $YEAR and end_dt year becomes $LAST_YEAR"
else
LAST_MONTH=$LAST_MONTH
echo "If current month is not January then last month is $LAST_MONTH"
fi

rm stage3sg.txt
if [ ! -s stage3sg.txt ]
then
echo "Creating File"
cat << EOF > stage3sg.txt

#MONTH=`date +%m`
#LAST_MONTH=$((`date +%m` -1))
#YEAR=`date +%Y`
#LAST_YEAR=$((`date +%Y` -1))
 start_dt $LAST_MONTH$YEAR
 end_dt $LAST_MONTH$LAST_YEAR

#echo start_dt `date "+%m%Y"`
EOF
fi
Posting here so other ppl can benefit
Thanks
J

Last edited by nixcraft; 10-04-2009 at 02:49 AM.
Reply With Quote
Reply

Tags
date , ksh , shell scripting , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell date command for manipulation and formatting date newbie4 Shell scripting 4 28-10-2008 07:33 PM
How to grep the date range sainipardeep Shell scripting 13 26-06-2008 07:34 AM
How to calculate date minus 72 hours? svinopas Shell scripting 1 09-10-2007 09:37 PM
script to ping + date dendi_rm Shell scripting 3 11-09-2007 10:42 AM
How can I rename a file by its date? warren Linux software 5 30-03-2006 12:11 PM


All times are GMT +5.5. The time now is 09:02 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38