This is how I would solve your problem in perl.
#!/usr/bin/perl # don't leave home without these two use strict; use warnings; # get time the perl-way (use gmtime instead of localtime for UTC) # http://perldoc.perl.org/functions/localtime.html # (the brackets around time are optional) my @fields = localtime(time); # extract the relevant values # the 'dumb' way - single scalars #my $day = $fields[3]; #my $month = $fields[4]; #my $year = $fields[5]; # the smart way - array slice my ($day, $month, $year) = @fields[3,4,5]; # add required offsets $month++; $year += 1900; # use format codes to zero-pad the month and day values (and also the # year - just to be sure; replace 'tar' and 'gz' with '%s'-format code # and a variable if you need them to be dynamic) my $dbfile = sprintf 'bugs_db_%04i-%02i-%02i.gz', $year, $month, + $day; my $webfile = sprintf 'bugzilla_%04i-%02i-%02i.tar.gz', $year, $month, + $day; print "Database file: $dbfile\n"; print "Archive: $webfile\n"; exit;
Output:
Database file: bugs_db_2015-07-02.gz
Archive:       bugzilla_2015-07-02.tar.gz

Why not use `date`?

There are 3 reasons - two have been mentioned already.

a) date may not be available - this may or may not concern you at all

b) it's really tricky to handle all side effects of calling external commands (e.g. properly catch the exit code, parse or hide StdErr, actually execute the correct command, ...)

not mentioned yet

c) The returned output of the external command may change unexpectedly. In your case it's not relevant, but you should keep that in mind when handling actual date values, parsing ifconfig output or any other command providing output that is influenced by the current language settings.

In order to properly execute an external command, you should provide a sane environment, evaluate the return code, catch StdOut, catch StdErr and maybe do some other stuff I forgot ... usually I try to find a way to do this stuff in pure-perl because it's easier to get it right. ;)


In reply to Re: Perl Variable Help. by Monk::Thomas
in thread Perl Variable Help. by arvindmarlabs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.