Is there a way to compare date strings , eg 20090302 , in shell
without having to resort to perl? I'm not talking about comparing file
dates.
Thanks for any help
B2003
They can be treated as numbers if they're in the format you gave above...
if [ $date1 -lt $date2 ]; then whatever; fi
Bjarni
--
INFORMATION WANTS TO BE FREE
Sorry , bad example. The format could be anything , eg 02Jan2009
B2003
If you seriously mean "anything" then I am surprised that Perl can do
it! If you can live with those formats accepted by the data command
you can use
date --date=02Jan2009 +%Y%m%d
(I'd turn this into a shell function to give the date in the desired
canonical form.)
BTW. You don't need to compare as numbers. Formats like +%Y%m%d and
the more readable +%Y-%m-%d also compare correctly as strings.
--
Ben.
But strings can only be compared as equals or not equals , you can't
do > or < in shell can you?
B2003
Your subject line references bash. bash has
[[ str1 < str2 ]]
along with > as well. The built-in version of test also supports the
< and > operators (but make sure you quote them!).
--
Ben.
And portably you could use expr or awk. But for both, you should
do:
expr "x$a" "<" "x$b" > /dev/null
awk 'BEGIN {exit(!(ARGV[1] < ARGV[2]))}' "x$a" "x$b"
to force litteral comparison (and avoid problems with some
values of $a/$b with expr).
--
Stéphane