Hi,
I want to set a cron job with the yesterday date in a unix server (solaris).
I tried TZ=CST+24 date +%Y%m%d but i had TZ=CST+24: not found
I tried `perl -mPOSIX -e 'print POSIX::strftime("%Y%m%d",localtime(time() - 86400)) '` it works but I'm not sure that all the clients have perl in their machines.
date -v and date -d don't work only -u and -a options work with date in unix:
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
...
A %(date-format)T format can be:
use to treat an argument as a date/time string and to format the
date/time according to the date-format as defined for the
date(1) command.
...
man ksh, section printf
So
printf "%(%Y-%m-%d)T\n" "yesterday"
would print ( for today = Fri Feb 13 12:01:29 CST 2015 )
2015-02-12
For ksh 93s+
Best wishes ... cheers, drl
PS:
TZ=CST+24 date +%Y%m%d
produced
20150212
in a system like:
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution : Solaris 10 10/08 s10x_u6wos_07b X86
with you format also specified? If you lack GNU date, get a copy at a trusted source. Or, you can search here for my time utility tm2tm.c, which does the same sort of things.
My recollection is that I tried to port GNU coreutils to get GNU date for Solaris. I was not successful.
It might be useful for the community here to have someone (with more expertise that I have) do a port. Judging from the posts here, I would say for Solaris and AIX, possibly HPUX.
I never found anything in sfw in Solaris to handle date arithmetic, but it's possible I missed something -- it was years before I stumbled on sfw I figured if the Sun/Oracle folks couldn't do the port, then it was probably difficult, or required a lot of extra work. The stuff in sfw seemed mostly like stand-alone items, e.g. ggrep ... cheers, drl
coreutils-8.11-sol10-sparc-local.gz GNU Coreutils are a set of basic file, shell, and text manipulation utilities for the GNU operating system that are expected to exist on every operating system. Previously, they were offered as three individual distributions: fileutils, shellutils, and textutils - installs in /usr/local. Dependencies: libiconv, libintl, gmp, and to obtain /usr/local/lib/libgcc_s.so.1 you will need to have installed libgcc-3.4.6 or gcc-3.4.6 or higher.
The programs included are
\[ cut false ls pathchk sha224sum sync unexpand
base64 date fmt md5sum pinky sha256sum tac uniq
basename dd fold mkdir pr sha384sum tail unlink
cat df groups mkfifo printenv sha512sum tee uptime
chcon dir head mknod printf shred test users
chgrp dircolors hostid mktemp ptx shuf timeout vdir
chmod dirname id mv pwd sleep touch wc
chown du install nice readlink sort tr who
chroot echo join nl rm split true whoami
cksum env kill nohup rmdir stat truncate yes
comm expand link nproc runcon stdbuf tsort
cp expr ln od seq stty tty
csplit factor logname paste sha1sum sum uname
coreutils-8.11.tar.gz Source Code. [Details]
Well, 'date' is just a program file, you can get a Gnu date and call it gdate or by path.
I wrote tm2tm as a more task oriented tool than date, not cluttered with clock setting. It is easy to compile and use. In this case, time input is clock, delta is back one day, format is YYYY-MM-DD:
Does /usr/sfw/bin on your solaris box have gdate or date ?
And. If you cannot change anything at why are you asking for something that implies some kind of code change?
This works on solaris10 and solaris11 - We use it in lots of scripts.
#!/bin/ksh
# usage /path/to/then.pl 7 for date seven days ago
# /path/to/then.pl 1 for yesterday
ago()
{
perl -e ' my $delta = $ARGV[0];
$delta*=86400;
$delta=time - $delta;
@t=localtime( $delta );
printf("%02d-%02d-%d", $t[4]+1, $t[3], $t[5]+1900); ' $1
}
echo $(ago $1)