Computing.Net > Forums > Unix > unix command

unix command

Reply to Message Icon

Original Message
Name: Devaraj (by Fidy)
Date: February 10, 2006 at 09:09:32 Pacific
Subject: unix command
OS: sun
CPU/Ram: 1280mb
Comment:

Hi,
How can we get the last month from today
in the format yymm.

for eg..if today is feb 06..the output shud be 0601.

Thanks for your help.


Regards,
Jern


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: February 10, 2006 at 15:57:13 Pacific
Subject: unix command
Reply: (edit)

This is one way. Two things to keep in mind:

1) I designed this for today so I subtracted 2000 to get a 2-digit year. Obviously, it won't work pre-2000.

2) Didn't test it for this month being January, but it should work if you expect December, 2005.

#!/bin/ksh

m=$(date '+%m')
m=$((m-=1))

y=$(date '+%Y')
if [[ m -eq 0 ]]
then # january
m=12
y=$((y-=1))
fi
printf "%02d%02d\n" $((y-2000)) $m


Report Offensive Follow Up For Removal

Response Number 2
Name: Devaraj (by Fidy)
Date: February 10, 2006 at 19:17:31 Pacific
Subject: unix command
Reply: (edit)

Thanks Nails..

Regards,
Jern


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: unix command

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge