Hi

Im writing a script where im using the date command in this format.

[Datestart=`date '+%m/%d/%Y':'%H:%M:%S'`]

Is their a way to force the date variable above to go back 5minutes??

I know their is a way in perl but i prefer writing this script in ksh

Dani AI

Generated

is right that the GNU date --date=... form works on Linux, but Solaris 10's /bin/date does not support --date (hence the "illegal option" error). Two simple, reliable alternatives that you can call from a ksh script on Solaris 10 are: use awk's time functions (Solaris nawk) or use a short Perl one-liner. Both subtract 300 seconds (5 minutes) from now and print the same timestamp format you asked for.

# nawk (Solaris), pure shell-friendly
Datestart=$(nawk 'BEGIN { t = systime() - 300; print strftime("%m/%d/%Y:%H:%M:%S", t) }')

# Perl fallback (works everywhere Perl is installed)
Datestart=$(perl -e 'use POSIX qw(strftime); print strftime("%m/%d/%Y:%H:%M:%S", localtime(time - 300)), "\n"')

Notes and tips: prefer nawk on Solaris (not plain old awk) because it exposes systime() and strftime() on Solaris 10. If nawk on your box doesn't have strftime, use the Perl line. If GNU coreutils is installed as gdate, you can also use gdate -d '5 minutes ago' (Linux-style) — check with command -v gdate. These approaches keep the logic in your ksh script via command substitution and avoid relying on GNU-only options in /bin/date.

Recommended Answers

All 3 Replies

$ date '+%m/%d/%Y':'%H:%M:%S' ; date --date='now -5 minutes' '+%m/%d/%Y':'%H:%M:%S'
08/26/2009:18:25:41
08/26/2009:18:20:41

Hi Salem

Tried that on Solaris 10

[date '+%m/%d/%Y':'%H:%M:%S' ; date --date='now -5 minutes' '+%m/%d/%Y':'%H:%M:%S'
09/01/2009:11:11:30
date: illegal option -- date=now -5 minutes
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
]

Looks like you've got a crappy date then, and you need to use the perl way you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.