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!

If you have GNU date:

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

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

To know your version of date, just run my command and see what your box reply.

A workaround, but again with GNU date:

#!/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

If you don't have GNU date, try this in bash:

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

It works. Excellent
I just don't have an idea of how to write something like you do.
Thanks a lot !