interpolate system() or exec()

Discussion in 'Perl Misc' started by N.Asenjo, Feb 10, 2006.

  1. N.Asenjo

    N.Asenjo Guest

    Hi,

    From a central syslog server logging to a mysql database I want to get
    only the errors with a perl script. The format looks like this:

    mysql> select host,priority,datetime,msg from logs limit 1;
    +------+----------+---------------------+-------------------------------
    | host | priority | datetime | msg
    |
    +------+----------+---------------------+------------------------------+
    | tux | info | 2006-01-29 14:13:01 | /USR/SBIN/CRON[24045]: (root)
    CMD (/sbin/adslscript) |
    +------+----------+---------------------+------------------------------+

    I want to get today's error messages, so the mysql query looks like
    this:

    mysql> select host,priority,datetime,msg from logs where priority in
    ('err','crit','alert') and datetime like '2006-02-10%' limit 2;

    And that works. My problem then:

    ---------code---------

    #!/usr/bin/perl
    use warnings;
    use strict;
    use DBI;

    # $date = system(`date +%Y-%m-%d`);
    # my $date = "2006-02-09";
    my @data;

    my $date = system("/bin/date +%Y-%m-%d");

    # Tried all these but do not work either
    # my $date = `date +%Y-%m-%d"`
    # chomp $datetime;
    # my $date = system("/bin/date +%Y-%m-%d");
    # my $date = system(q{"/bin/date +%Y-%m-%d"},);
    # @args = ( "date +%Y-%m-%d" );
    # $date = exec { $args[0] } @args;

    my $query = "SELECT host,priority,datetime,msg FROM logs where datetime
    like '$date%' and priority in ('err','crit','alert')";
    my $dbh = DBI->connect('dbi:mysql:syslog', 'user', 'passwd')
    or die DBI->errstr;

    my $sth = $dbh->prepare($query) or die $sth->errstr;
    $sth->execute() or die $sth->errstr;

    # read the matching records and print them out
    while (@data = $sth->fetchrow_array()) {
    my $host = $data[0];
    my $priority = $data[1];
    my $datetime = $data[2];
    my $msg = $data[3];
    print "$host $datetime $priority $msg\n";
    }

    $sth->finish;
    $dbh->disconnect;

    -----------------end code
    and when i run it:

    perl datetime.pl
    Name "main::sth" used only once: possible typo at datetime.pl line 23.
    2006-02-10

    and there I am, back to my shell.

    If I hardcode
    $date = "2006-02-10";
    Then it interpolates and runs fine. This is useless because I want to
    use
    the script in a cronjob. This is a sysadmin script, will not be run
    thousands of times in a cgi, so no danger of cross-side scripting or
    anything like that.

    Anyway, I am stuck. Any help is greatly appreciated.
    --
    Groeten,
    J.I.Asenjo
     
    N.Asenjo, Feb 10, 2006
    #1
    1. Advertising

  2. N.Asenjo

    N.Asenjo Guest

    Hi, N.Asenjo,

    ok, I found it (finally):

    > my $date = system("/bin/date +%Y-%m-%d");


    this has to be backticks instead of system():

    chomp(my $date = `date +%Y-%m-%d`);

    it works like a charm.

    --
    Groeten,
    J.I.Asenjo
     
    N.Asenjo, Feb 10, 2006
    #2
    1. Advertising

  3. N.Asenjo

    Anno Siegel Guest

    N.Asenjo <> wrote in comp.lang.perl.misc:
    > Hi, N.Asenjo,
    >
    > ok, I found it (finally):
    >
    > > my $date = system("/bin/date +%Y-%m-%d");

    >
    > this has to be backticks instead of system():
    >
    > chomp(my $date = `date +%Y-%m-%d`);
    >
    > it works like a charm.


    ....which is explicitly described in the documentation of system().

    There is no need for an external command.

    use POSIX;
    my $date = strftime "%Y-%m-%d", localtime;

    Anno
    --
    $_='Just another Perl hacker'; print +( join( '', map { eval $_; $@ }
    'use warnings FATAL => "all"; printf "%-1s", "\n"', 'use strict; a',
    'use warnings FATAL => "all"; "@x"', '1->m') =~
    m|${ s/(.)/($1).*/g; \ $_ }|is),',';
     
    Anno Siegel, Feb 10, 2006
    #3
  4. N.Asenjo

    N.Asenjo Guest

    Hi, Anno Siegel,
    > N.Asenjo <> wrote in comp.lang.perl.misc:


    > > > my $date = system("/bin/date +%Y-%m-%d");

    > >
    > > this has to be backticks instead of system():
    > >
    > > chomp(my $date = `date +%Y-%m-%d`);


    > ...which is explicitly described in the documentation of system().


    you are right, I had already read it but as it turns out not correctly
    used it at first. It all is a part of the learning process.

    > There is no need for an external command.
    >
    > use POSIX;
    > my $date = strftime "%Y-%m-%d", localtime;


    well, look, this is useful. Thanks!
    --
    Groeten,
    J.I.Asenjo
     
    N.Asenjo, Feb 10, 2006
    #4
  5. "N.Asenjo" <> wrote in
    news::

    > # Tried all these but do not work either
    > # my $date = `date +%Y-%m-%d"`
    > # chomp $datetime;
    > # my $date = system("/bin/date +%Y-%m-%d");
    > # my $date = system(q{"/bin/date +%Y-%m-%d"},);
    > # @args = ( "date +%Y-%m-%d" );
    > # $date = exec { $args[0] } @args;


    You are expected to read the documentation before posting here.

    Even if you did not know about the POSIX module, you should have known
    that Perl provides

    perldoc -f time
    perldoc -f localtime
    perldoc -f gmtime

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 10, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    492
  2. Replies:
    0
    Views:
    313
  3. Samuel Williams
    Replies:
    3
    Views:
    112
    Brian Candler
    Feb 9, 2011
  4. Mr P
    Replies:
    21
    Views:
    219
    Brian McCauley
    Dec 28, 2005
  5. Chris M.

    Can't Interpolate variable into a string

    Chris M., Dec 17, 2008, in forum: Javascript
    Replies:
    6
    Views:
    106
    Thomas 'PointedEars' Lahn
    Dec 19, 2008
Loading...

Share This Page