yesterday date month/date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting yesterday date month/date
# 1  
Old 06-24-2008
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  
Old 06-24-2008
If you have GNU date:

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

# 3  
Old 06-24-2008
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  
Old 06-24-2008
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  
Old 06-24-2008
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  
Old 06-24-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

3. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

4. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

5. Shell Programming and Scripting

How to get tomorrow,yesterday date from date Command

Hi I want to get tomorrow and yesterday date from date command. My shell is KSH and server is AIX. I tried several options, but unable to do. Please help on this. Regards Rajesh (5 Replies)
Discussion started by: rajeshmepco
5 Replies

6. Shell Programming and Scripting

[Solved] Replace yesterday date with today's date except from the first line

Hello, I have a file like this: 2012112920121130 12345620121130msABowwiqiq 34477420121129amABamauee e7748420121130ehABeheheei in case the content of the file has the date of yesterday within the lines containing pattern AB this should be replaced by the current date. But if I use... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

7. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

8. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

9. Shell Programming and Scripting

Get yesterday's date in year-month-day format?

Dear All, Actually, i'm doing some reporting job and i need to pass yesterday's date in Year-Month-Day format(e.g. 2009-06-10) to another program for generating 2009-06-10 report. to get today's date, it's easy to just date '+%Y%m%d' , but no idea how can i get this kind of format for... (2 Replies)
Discussion started by: tiger2000
2 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question