[H-GEN] Reading from and writing to, environment variable from PERL
Greg Black
gjb at gbch.net
Thu Sep 11 07:49:43 EDT 2003
[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]
On 2003-09-10, Andrae Muys wrote:
> Cunningham, Conor wrote:
> >I am writing some scripts at work, to go into a ~/bin dir full of tools.
> >This tool will take a date which is stored in a local/environment var
> >called $data_period, in the format yyyy-mm-dd.
> >
> >It will then, depending on parameters, output the date per the users
> >wishes. i.e. yyyymm.
>
> 1) I'm pretty sure there is already a unix utility that does this, so
> you might want to check your man pages (or just wait for Mark/Greg to
> pipe-up with the answer ;).
Oh well, the Unix utility to do this is called date(1). Sadly,
although all modern variants of date(1) will do what you want,
the exact syntax does vary slightly between implementations.
For the version that I have handy right now[1], the following
will convert a date in the form "11 Sep 2003" to "2003-09":
date -j -f "%d %b %Y" "11 Sep 2003" "+%Y-%m"
Turning that into something useful in a script is a trivial
exercise for the reader.
The formats to be used for the argument to -f and for the "+"
output format are described in strftime(3).
Cheers, Greg
[1] The date(1) command I illustrated is supposed to comply with
IEEE Std1003.2 (aka POSIX.2), so this should work on most
modern(ish) systems.
--
Greg Black <gjb at gbch.net> <http://www.gbch.net/gjb.html>
GPG signed mail preferred; further information in headers.
--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'. See http://www.humbug.org.au/
More information about the General
mailing list