Errors in Perl Function

A

abhi147

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
 
A

Andrew DeFaria

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.
 
J

John W. Krahn

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
 
H

hymie!

In our last episode, the evil Dr. Lacto had captured our hero,
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 (e-mail address removed)
===============================================================================
 
D

Darren Dunham

John W. Krahn said:
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.
 
P

Peter J. Holzer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top