assign yesterday's date to variable

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support assign yesterday's date to variable
# 1  
Old 08-20-2010
assign yesterday's date to variable

Hi,

i am trying to assign yesterday's date to a variable on below system -

Machine hardware: sun4u
OS version: 5.9
Processor type: sparc

usr> setenv dt `date +"%Y%m%d"`
usr> echo $dt
20100820

i am able to assign today's date but similarly i want to assign yesterday's date.

Plz help

Thankx!!
# 2  
Old 08-20-2010
Try this:

yest.sh
Code:
date '+%m %d %Y' |  
{  
read MONTH DAY YEAR 
DAY=`expr "$DAY" - 1`  
case "$DAY" in  
        0)  
           MONTH=`expr "$MONTH" - 1`  
                case "$MONTH" in  
                        0)  
                           MONTH=12  
                           YEAR=`expr "$YEAR" - 1`  
                        ;;  
                esac  
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`  
esac  
echo "$MONTH$DAY$YEAR"  
}

other script:
Code:
yest=`yest.sh`
echo $yest

Code:
# sh somescript.sh
08192010


Last edited by Ikon; 08-20-2010 at 11:44 AM..
# 3  
Old 08-20-2010
Thankx for the reply but i am looking for result in YYYYMMDD format
# 4  
Old 08-20-2010
and you cant figure that out?

Look here!
Code:
....
echo "$MONTH$DAY$YEAR"

# 5  
Old 08-20-2010
got it!! thankx!!
# 6  
Old 08-20-2010
Quote:
Originally Posted by rahulbahulekar
usr> setenv dt `date +"%Y%m%d"`
usr> echo $dt
20100820
try:
Code:
info date

maybe:
Code:
date -d yesterday
date --date=yesterday
date -d '1 day ago'

# 7  
Old 08-21-2010
Default date in Solaris don't support these options, such as: show yesterday, show several days/times ago, etc.

You need install GNU date in Solaris. It will make easy for your life.

the GNU date is in coreutils package, which you can download from sunfreeware.com.au and install directly.
This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

ksh PS4 variable assign to `date` output

Hi guys, Is there a way to assign curent time to PS4 variable in ksh. My goal is to have each line produced by 'set -x' command to have a time stamp. Here is my code: $cat test #!/usr/bin/ksh export PS4="`date` " set -x echo "TRACE LINE ONE" echo "I WILL SLEEP FOR 10 SEC" sleep 10... (2 Replies)
Discussion started by: aoussenko
2 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

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

5. 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

6. Shell Programming and Scripting

assign date to a variable

hi, i've just started learning a bit of shell scripting and i wanted to know how to assign the value of the day of the month (i think the command for that is date +%d) to a variable say 'd' so that i can display 'st', 'nd', 'rd' and 'th' depending upon the value of that variable. i'd... (5 Replies)
Discussion started by: xXWiLdAcEXx
5 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

Getting yesterday `date`

Hi, `date` command will give the current days date. Is there any command to get the previous day date? I need the previous day value in my script. Ahamed. (1 Reply)
Discussion started by: ahamed
1 Replies

9. 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

10. Shell Programming and Scripting

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! (5 Replies)
Discussion started by: skully
5 Replies
Login or Register to Ask a Question