- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- date (how to get date of yesterday)
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:22 PM
04-30-2008 02:22 PM
if i run date i get:
$ a=`date "+%b %d"`
$ echo $a
Apr 30
how can i get one day before?
i mean, i need to get the value of yesterdad (according to current date) to keep it in a variable ...
in this case, today is "Apr 30"
I need get "Apr 30"
if today were "May 1" , i woould need to get "Apr 30" ..
how can i do that
plase let me know :)
Thanks in Advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:36 PM
04-30-2008 02:36 PM
Solution# /usr/local/bin/when
# usage "when 7" to go back 7 days, "when 14" to go back 14, etc.
# useless to go back more than a month
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
BACK=$1
THEN=$((`date +%d` - $BACK))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ $THEN -le "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
SHIFT=$(( $THEN * -1 + 1 ))
shift $(($# - $SHIFT))
THEN=$1
fi
TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
echo $THEN $MONTH $TMONTH $YEAR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:38 PM
04-30-2008 02:38 PM
Re: date (how to get date of yesterday)
$ cat > fecha
#!/bin/ksh
# /usr/local/bin/when
# usage "when 7" to go back 7 days, "when 14" to go back 14, etc.
# useless to go back more than a month
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
BACK=$1
THEN=$((`date +%d` - $BACK))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ $THEN -le "0" ]
then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]
then
MONTH=12
YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
SHIFT=$(( $THEN * -1 + 1 ))
shift $(($# - $SHIFT))
THEN=$1
fi
TMONTH=${MONTHS[MONTH]}
# uncomment next line for debugging
echo $THEN $MONTH $TMONTH $YEAR
$ ./fecha
sh: ./fecha: Execute permission denied.
$ chmod 755 fe*
$ ./fecha
./fecha[7]: 30 - : more tokens expected
$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:40 PM
04-30-2008 02:40 PM
Re: date (how to get date of yesterday)
$ ./fecha
sh: ./fecha: Execute permission denied.
$ chmod 755 fe*
$ ./fecha
./fecha[7]: 30 - : more tokens expected
$ fecha `date`
./fecha[7]: 30 - Wed: bad number
$ fecha `date "+%b %d"`
./fecha[7]: 30 - Apr: bad number
$ fecha `date`
./fecha[7]: 30 - Wed: bad number
$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:41 PM
04-30-2008 02:41 PM
Re: date (how to get date of yesterday)
use Perl;
# perl -le 'print scalar localtime time - 86400'
# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)'
If you use the second snippet, you can use the same formatting directives you use with the Unix 'date' command; e.g.:
# perl -MPOSIX -le 'print strftime "%b %d",localtime(time-86400)'
Apr 29
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:43 PM
04-30-2008 02:43 PM
Re: date (how to get date of yesterday)
$ perl -MPOSIX -le 'print strftime "%b %d",localtime(time-86400)'
Apr 29
$ perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-86400)'
04/29/2008
$ perl -le 'print scalar localtime time - 86400'
Tue Apr 29 18:42:31 2008
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2008 02:43 PM
04-30-2008 02:43 PM
Re: date (how to get date of yesterday)
#! /usr/bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
OFFSET=${1:-1}
case $OFFSET in
*[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac
eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
month=$((month - 1))
if (( month == 0 )) ;then
year=$((year - 1))
month=12
fi
set -A days `cal $month $year`
xday=${days[$(( ${#days[*]}-1 ))]}
day=$((xday + day))
fi
print $year-$month-$day
print $month/$day/${year#??}