Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 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. |
| Sponsored Links | ||
|
|
|
|||
|
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 Thanks J Last edited by nixcraft; 10-04-2009 at 02:49 AM. |
![]() |
| Tags |
| date , ksh , shell scripting , unix |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |