The UNIX and Linux Forums  

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



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting yesterday DATE osymad Shell Programming and Scripting 19 09-23-2009 03:39 PM
Help, I need to get the last date of previous month sirrtuan Shell Programming and Scripting 11 10-14-2008 06:59 AM
last month end date vanathi UNIX for Advanced & Expert Users 7 03-21-2008 04:17 PM
How to show yesterday date wind_n_cloud Shell Programming and Scripting 1 02-16-2005 10:51 PM
get yesterday date in yyyymmdd format hk_newbie UNIX for Dummies Questions & Answers 2 12-14-2001 03:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 06-24-2008
skully skully is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 46
yesterday date month/date

Hi expert,

I want to retrieve yesterday su log.
How to calculate and assign variable value ( 06/23 ) in myVariable ?


#!/bin/sh

myVariable=yesterday date in month/date

cat /var/adm/sulog | grep $myVariable > file.txt



many thanks!
  #2 (permalink)  
Old 06-24-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
If you have GNU date:


Code:
myVariable=$(date "+%m/%d" --date "now 1 day ago")

  #3 (permalink)  
Old 06-24-2008
skully skully is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 46
I don't think I have that. How to check ?

I can extract the date , let say 24. But how to do -1

How to correct below code's syntax in /bin/sh

myDate= `date -u +%d`
myMonth=`date -u +%m`
myYear=`date -u +%Y`
myAnotherMonth=`date -u +%b`

if [ $myDate == 1 && ( $myAnotherMonth == Sep || $myAnotherMonth == Apr || $myAnotherMonth == Jun || $myAnotherMonth == Nov || $myAnotherMonth == Feb ) ]
then
yesterday = 30

elseif [ $myDate == 1 && $myAnotherMonth == Feb]

yesterday=28 # I dont worry about month end with 29

else

yesterday = $myDate - 1

fi

cat /var/adm/sulog | grep "$myMonth\/$yesterday" | grep -v grep > $myYear$myMonth$yesterday_sulog.txt

Last edited by skully; 06-24-2008 at 04:28 AM..
  #4 (permalink)  
Old 06-24-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
To know your version of date, just run my command and see what your box reply.

A workaround, but again with GNU date:

Code:
#!/bin/bash 

STAMP_TODAY=$(date --utc --date "$1" +%s)
STAMP_YESTERDAY=$((STAMP_TODAY-86400))
DTE_YESTERDAY=$(date --utc --date "1970-01-01 $STAMP_YESTERDAY sec" "+%m/%d")

echo $DTE_YESTERDAY

  #5 (permalink)  
Old 06-24-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 441
If you don't have GNU date, try this in bash:


Code:
TODAY_D=$(date -u +%d)
TODAY_M=$(date -u +%m)
LAST_DAY_OF_M=(-- 31 28 31 30 31 30 31 31 30 31 30 31)
if [[ $TODAY_D == "01" ]];then
        if [[ $TODAY_M == "01" ]];then
                PREVIOUS_M="12"
        else
                PREVIOUS_M=$((TODAY_M-1))
        fi
        printf "%02d/%s" $PREVIOUS_M ${LAST_DAY_OF_M[$PREVIOUS_M]}
else
        printf "%s/%s" $TODAY_M $((TODAY_D-1))
fi
exit 0


Last edited by ripat; 06-24-2008 at 06:39 AM.. Reason: Forgot to take care of January!
  #6 (permalink)  
Old 06-24-2008
skully skully is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 46
It works. Excellent
I just don't have an idea of how to write something like you do.
Thanks a lot !

Last edited by skully; 06-24-2008 at 06:55 AM.. Reason: forgot to include Excellent
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 On




All times are GMT -4. The time now is 05:21 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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0