Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-01-2006
Registered User
 

Join Date: Dec 2005
Posts: 9
Date format - Shell scripting gurus please help

Guys:
I have the following script (from this forum) to calculate yesterdays date.
However the format of the date that is output is yyyymd if the resultant date is single digit or the resultant month is a single digit month ( 01 - 09 ). How can I get the output to show the date in the following format yyyymmdd always instead of yyyymd

Below is the script:
date '+%Y %m %d' |#echo here shows date in the yyyymmdd format
{
read YEAR MONTH DAY #echo here shows date in the yyyymmdd format
DAY1=`expr "$DAY" - 1`
case "$DAY1" in
0)
MONTH=`expr "$MONTH" - 1`
echo $MONTH
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY1=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
DAY2=`expr "$DAY1" - 1`
echo $DAY1 # shows date in yyyymdd format if DAY1 is 200612 instead of 20060102
echo $DAY2 # shows date in yyyymdd format if DAY2 is 200611 instead of 20060101
esac
}

regards, m
Sponsored Links
  #2 (permalink)  
Old 02-01-2006
Registered User
 

Join Date: Feb 2006
Location: Mumbai, India
Posts: 25
Add a little if condition before you expr to check if the resulting date is between 1 -9 and if it is then add a 0 as prefix of the $DAY variable
  #3 (permalink)  
Old 02-01-2006
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
In ksh,
typeset -Z2 MONTH DAY
  #4 (permalink)  
Old 02-02-2006
Registered User
 

Join Date: Jan 2006
Posts: 8
Hi ,
have u used following command

date -d "yesterday" '+%Y%m%d'
Sponsored Links
Closed Thread

Bookmarks

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

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


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
date issue-find prevoius date in a patricular format bsandeep_80 UNIX for Advanced & Expert Users 3 11-15-2007 08:42 PM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-28-2006 12:30 AM
difference between AIX shell scripting and Unix shell scripting. haroonec Shell Programming and Scripting 2 04-12-2006 09:12 AM
Help with shell script - Unix Gurus calling ravred Shell Programming and Scripting 4 01-26-2006 04:20 AM
convert date in lilian format for shell (AIX) chocolate AIX 2 11-09-2004 04:48 AM



All times are GMT -4. The time now is 09:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0