Errors in Perl Function

Discussion in 'Perl Misc' started by abhi147@gmail.com, Nov 27, 2006.

  1. Guest

    Hi all ,

    I have a shell script which has a small function to find previous
    day's date , which is a perl function.
    When I run this script on the command line , the script runs
    successfully . Whereas when I run it as a cron job , it gives errors .

    The perl function in the shell script is :

    ### To get yesterday's date ###
    function get_yesterday {
    perl << "EOF"
    my ($mday, $mon, $year) = (localtime(time()-86400))[3,4,5];
    $mon++;
    $year += 1900;
    printf("%04d-%02d-%02d\n", $year, $mon, $mday);
    EOF
    }
    yesterday=$(get_yesterday)

    The errors encountered were :

    $ ./test.sh
    ../test.sh: function: not found
    2006-11-23
    ../test.sh: syntax error at line 15: `}' unexpected

    Can anyone please tell me .. that do I need to do something extra to
    run this script through crontab ?


    Thanks
     
    , Nov 27, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi all ,
    >
    > I have a shell script which has a small function to find previous
    > day's date , which is a perl function.
    > When I run this script on the command line , the script runs
    > successfully . Whereas when I run it as a cron job , it gives errors .
    >
    > The perl function in the shell script is :
    >
    > ### To get yesterday's date ###
    > function get_yesterday {
    > perl << "EOF"
    > my ($mday, $mon, $year) = (localtime(time()-86400))[3,4,5];
    > $mon++;
    > $year += 1900;
    > printf("%04d-%02d-%02d\n", $year, $mon, $mday);
    > EOF
    > }
    > yesterday=$(get_yesterday)
    >
    > The errors encountered were :
    >
    > $ ./test.sh
    > ./test.sh: function: not found
    > 2006-11-23
    > ./test.sh: syntax error at line 15: `}' unexpected
    >
    > Can anyone please tell me .. that do I need to do something extra to
    > run this script through crontab ?
    >
    >
    > Thanks
    >

    Lacking any formal shebang line your script is probably being executed
    by Bourne Shell which lacks function (and and probably isn't that happy
    about the $(command) syntax either). Try adding #!/bin/bash as the first
    line.


    --

    Andrew DeFaria <http://defaria.com>
    If a mime is arrested do they tell him he has the right to talk?
     
    Andrew DeFaria, Nov 27, 2006
    #2
    1. Advertising

  3. wrote:
    >
    > I have a shell script which has a small function to find previous
    > day's date , which is a perl function.
    > When I run this script on the command line , the script runs
    > successfully . Whereas when I run it as a cron job , it gives errors .
    >
    > The perl function in the shell script is :
    >
    > ### To get yesterday's date ###
    > function get_yesterday {
    > perl << "EOF"
    > my ($mday, $mon, $year) = (localtime(time()-86400))[3,4,5];
    > $mon++;
    > $year += 1900;
    > printf("%04d-%02d-%02d\n", $year, $mon, $mday);
    > EOF
    > }
    > yesterday=$(get_yesterday)
    >
    > The errors encountered were :
    >
    > $ ./test.sh
    > ./test.sh: function: not found
    > 2006-11-23
    > ./test.sh: syntax error at line 15: `}' unexpected
    >
    > Can anyone please tell me .. that do I need to do something extra to
    > run this script through crontab ?


    Since this is a shell script why not use date?

    $ date "+%Y%m%d"; date -d yesterday "+%Y%m%d"
    20061127
    20061126


    But if you really must use Perl:

    $ perl -MPOSIX -le'
    print for strftime( "%Y%m%d", localtime ),
    strftime( "%Y%m%d", localtime time() - 86400 )
    '
    20061127
    20061126



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 27, 2006
    #3
  4. hymie! Guest

    In our last episode, the evil Dr. Lacto had captured our hero,
    "John W. Krahn" <>, who said:

    >Since this is a shell script why not use date?
    >
    >$ date "+%Y%m%d"; date -d yesterday "+%Y%m%d"
    >20061127
    >20061126


    date -d is a gnu-specific extension that is not necessarily portable
    across all systems.

    >Perl isn't a toolbox, but a small machine shop where you can special-order
    >certain sorts of tools at low cost and in short order. -- Larry Wall


    An old co-worker had a similar quote, which I don't know where he found:
    Unix is all of the tools you need to build the machine you thought you bought.

    hymie! http://www.smart.net/~hymowitz
    ===============================================================================
     
    hymie!, Nov 27, 2006
    #4
  5. John W. Krahn <> wrote:

    > But if you really must use Perl:


    > $ perl -MPOSIX -le'
    > print for strftime( "%Y%m%d", localtime ),
    > strftime( "%Y%m%d", localtime time() - 86400 )
    > '
    > 20061127
    > 20061126


    That function may fail in environments with daylight saving time if
    executed near potential time change points. Since many folks schedule
    cron jobs for wee hours of the morning, that's a very real possibility.

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Nov 28, 2006
    #5
  6. On 2006-11-28 18:40, Darren Dunham <> wrote:
    > John W. Krahn <> wrote:
    >
    >> But if you really must use Perl:

    >
    >> $ perl -MPOSIX -le'
    >> print for strftime( "%Y%m%d", localtime ),
    >> strftime( "%Y%m%d", localtime time() - 86400 )
    >> '
    >> 20061127
    >> 20061126

    >
    > That function may fail in environments with daylight saving time if
    > executed near potential time change points.


    More precisely, it will fail if there was a DST within the last 24 hours
    and the script is run between 23:00 and 24:00 or 00:00 and 01:00
    depending on the direction of the switch.

    > Since many folks schedule cron jobs for wee hours of the morning,
    > that's a very real possibility.


    Depends on what you mean by "wee hours of the morning". To me 23:00 to
    01:00 is more like "late evening". However, I do like to schedule cron
    jobs just after midnight (especially those of the "gather statistics
    from the previous day" variety) so I guess I have some cronjobs to
    check.

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Dec 3, 2006
    #6
    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. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    985
    Mark Goldin
    Jan 17, 2004
  2. SenthilVel
    Replies:
    0
    Views:
    961
    SenthilVel
    Jun 7, 2006
  3. George1776

    Out-of-memory errors and caching errors.

    George1776, Aug 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,315
    George1776
    Sep 14, 2006
  4. Lance Wynn
    Replies:
    1
    Views:
    1,876
    Lance Wynn
    Feb 3, 2008
  5. yawnmoth
    Replies:
    97
    Views:
    4,757
    Bent C Dalager
    Feb 27, 2009
Loading...

Share This Page