Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to output yesterday's day of the month

dev44
Regular Advisor

Script to output yesterday's day of the month

Hi,

I need to output yesterday's day of the month. But, I need it to print out as "01" not "1". Presently I do: d=`expr $(date +%d) - 01` but all this does is prints out a 1. I need a 01.

Thanks,
whatever
5 REPLIES
Steven E. Protter
Exalted Contributor

Re: Script to output yesterday's day of the month

Shalom,

Check out the man page on printf.

Here is an example of formatted output.

ipcs | awk '{total=total+$5};END {printf "%6.3f\n",total/1024/1024/1024 }'

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
dev44
Regular Advisor

Re: Script to output yesterday's day of the month

Found a way around the problem...with if/then statement
whatever
Dennis Handly
Acclaimed Contributor

Re: Script to output yesterday's day of the month

Instead of using printf(1), you can use the shell directly:
typeset -Z2 d=$(( $(date +%d) - 1 ))

Of course this doesn't work too well on the first or the first of the year.
Fredrik.eriksson
Valued Contributor

Re: Script to output yesterday's day of the month

A little google revealed this:
# echo `(export TZ=XYZ+24; date "+%Y-%m-%d")`

Look here for more :)
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=9251

Best regards
Fredrik Eriksson
Highlighted
Dennis Handly
Acclaimed Contributor

Re: Script to output yesterday's day of the month

>Fredrik: A little google revealed this:

If you look a litter further, you'll find this is not supported, safe nor work all the time.

And you can simplify it to just:
TZ=XYZ+24 date "+%Y-%m-%d"