UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 10th, 2006, 06:17 AM
itsmeavi itsmeavi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 itsmeavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 40 sec
Reputation Power: 0
Problem with Shell script

I m newbie to unix shell scripting ... and i m in a kind of trouble right now. I want that if the date is less than 10 then there should be two spaces between month and date otherwise only single space...

Can anybody help me out pls

Code:
mm=`date +%b`
dd=`date +%d`
dd=`expr $dd - 1`
dt=`echo  $mm $dd
`

Reply With Quote
  #2  
Old August 10th, 2006, 06:46 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 727 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 3 h 40 m 36 sec
Reputation Power: 315
I am sure we can do something for you ... on a side note, you are looking to print out yesterdays date?

Reply With Quote
  #3  
Old August 10th, 2006, 06:50 AM
itsmeavi itsmeavi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 itsmeavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 40 sec
Reputation Power: 0
yeah i want yesterdays date though if date is less than 10 i.e. single digit then i want two spaces between month and date ...

Reply With Quote
  #4  
Old August 10th, 2006, 07:22 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 727 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 3 h 40 m 36 sec
Reputation Power: 315
Ok, here we go ...
Code:
#!/usr/bin/sh
#
# Get month and days of month and year
yy=`date +%Y`
mm=`date +%m`
dd=`date +%d`
# Get yesterday
dd=`expr $dd - 1`
# Have we dropped into previous month?
if [ $dd -lt 1 ]
then
# Get previous month
  mm=`expr $mm - 1`
# Have we dropped into previous year?
  if [ $mm -lt 1 ]
  then
    mm=12
    yy=`expr $yy - 1`
  fi
# Now, feed that new month into cal to get last day number
  dd=`cal $mm $yy | tail -1 | awk '{print $(NF)}'`
fi
# If single digit, pad with a space
if [ $dd -lt 10 ]
then
  dd=" $dd"
fi
# Now use similar method to get the month name
mm=`cal $mm $yy | head -1| awk '{print $1}' | cut -c1-3`
dt="$mm $dd"

Reply With Quote
  #5  
Old August 10th, 2006, 07:37 AM
itsmeavi itsmeavi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 itsmeavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 40 sec
Reputation Power: 0
thnx man however its not appending space infront ....

Reply With Quote
  #6  
Old August 10th, 2006, 08:13 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 727 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 3 h 40 m 36 sec
Reputation Power: 315
Ahhh! I think I see what you mean:

Using . to denote a space -

Code:
Aug.10
.Aug.9


Is what you are after?

Last edited by SimonJM : August 10th, 2006 at 08:13 AM. Reason: remove extra space char

Reply With Quote
  #7  
Old August 10th, 2006, 08:32 AM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,152 kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)kicken User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1Folding Points: 115670 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 2 Weeks 6 Days 16 h 47 m 32 sec
Reputation Power: 546
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
this is what he wants:

Code:
Aug.10
Aug..9
__________________
Spidermonkey Tutorial

http://wiser.aoeex.com/ - Long term project (offline due to evil crawlers and lack of content)
http://www.aoeex.com/gmap.php - Put yourself on the map

Reply With Quote
  #8  
Old August 10th, 2006, 09:54 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 727 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 3 h 40 m 36 sec
Reputation Power: 315
It works for me ... try:
echo "$dt"

Reply With Quote
  #9  
Old August 10th, 2006, 10:55 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 2 m 57 sec
Reputation Power: 48
printf works, too:
Code:
mm=`date +%b`
dd=`date +%d`
dd=`expr $dd - 1`
dt=`printf "%s %d"  $mm $dd`


%d will format the number correctly.

Q: what happens on the first day of the month...? When you want yesterday?

GNU date does simple date arithmetic operations, otherwise use perl. Example for yesterday:

Code:
#!/bin/ksh

yesterday()
{
  perl -e '
     # today minus 86400 seconds is this time yesterday
 	 $yesterday = time - 86400;  
 	 
 	 # this line gets the month name
 	 $month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime $yesterday)[4]];
 	 
 	 # day of the month
 	 $day = (localtime $yesterday)[3];
 	 print "$month $day", "\n";
  ' 
}

echo "Day before today was $( yesterday )"

Reply With Quote
  #10  
Old August 11th, 2006, 12:44 AM
itsmeavi itsmeavi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 itsmeavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 40 sec
Reputation Power: 0
The space is still missing even with printf

I guess this problem is with the version of unix mine is:
Quote:
SunOS chbdat3 5.9 Generic_118558-06 sun4u sparc SUNW,Sun-Fire

Reply With Quote
  #11  
Old August 11th, 2006, 12:52 AM
itsmeavi itsmeavi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 7 itsmeavi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 40 sec
Reputation Power: 0
Thanx a lot everybody though i worked out one solution last night..

Code:
mm=`date +%b`
dd=`date +%d`
dd=`expr $dd - 1`
if [ "$dd" -ge "10" ]
then 
dt=`echo "$mm $dd"` 
else 
dt=`echo "$mm  $dd"` 
fi

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Problem with Shell script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |