PDA

View Full Version : I'm sure you've seen this before, but....Unitialized Value/Cron


crimper1
09-23-02, 15:52
All,
First off, would anybody know why a script gives differernt results when from a cron job then from the command line? (That's one of my problems, maybe it will be corrected if I fix the problem below.)

As I'm sure you've all read and fixed a thousand times before, I'm having a 'Use of initialized value at line #" error after running my program (again from the cron, not from the command line). The code around the error is as follows:
-----------------
@file_list = qw(file1 file2 file3);
foreach $file (@file_list) {
$count = `grep "<a string>" $file | wc -l`;
if ($count eq " 0 ") {
system (echo "no matches in $file" >> another_file);
} else {
system (echo "$count matches found in $file >> another_file);
}
}
}
-------------------
I'm getting the "unitialized value" error on the 3rd line of the above code AFTER the first foreach loop. When I run the script from the command line, it correctly writes "# matches found in file" but from the cron, it incorrectly writes "no matches found". BTW, I'm not using any "use" or "my" statements before/after this block.

Any ideas?

Bernd Dulfer
09-25-02, 08:55
> First off, would anybody know why a script gives
> differernt results when from a cron job then from the
> command line? (That's one of my problems, maybe it
> will be corrected if I fix the problem below.)

A cron job has a different environment. You have to specify programs with their full path, for instance.

> The code around the error is as follows:


@file_list = qw(file1 file2 file3);
foreach $file (@file_list) {
$count = `grep "<a string>" $file | wc -l`;
if ($count eq " 0 ") {
system (echo "no matches in $file" >> another_file);
} else {
system (echo "$count matches found in $file >> another_file);
}
}
}


> Any ideas?

It's not easy to find an error in a piece of made-up perl.
Some thoughts:
- Strip the program down to the smallest running piece of code.
- From your code it looks like you are not using 'strict'. Use it!
- Consider visiting www.perlmonks.org.
rgds