|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
I am sure we can do something for you ... on a side note, you are looking to print out yesterdays date?
|
|
#3
|
|||
|
|||
|
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 ...
|
|
#4
|
|||
|
|||
|
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"
|
|
#5
|
|||
|
|||
|
thnx man however its not appending space infront ....
![]() |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
||||
|
||||
|
this is what he wants:
Code:
Aug.10 Aug..9
__________________
Spidermonkey Tutorial http://www.aoeex.com/gmap.php - Put yourself on the map |
|
#8
|
|||
|
|||
|
It works for me ... try:
echo "$dt" |
|
#9
|
|||
|
|||
|
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 )"
|
|
#10
|
|||
|
|||
|
The space is still missing even with printf
I guess this problem is with the version of unix mine is: Quote:
|
|
#11
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Problem with Shell script |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|