Home > Archive > PERL Beginners > November 2007 > error on simple system command
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
error on simple system command
|
|
| Lerameur 2007-11-26, 10:02 pm |
| Hello,
I am trying to use this two line script. The command by itself works,
when I run this script, I get error message:
Use of uninitialized value in concatenation (.) or string at ./
find_date.pl line 8.
line 8: my $file_to_print = system "ls -lrt /test/*log | tail -1 |
awk {'print $9'}";
print $file_to_print;
then after the error message an erroneous output, showing the correct
file but with all the fields...
how Do I get the script to function as if I where to implement the
command directly.
Thanks,
ken
| |
| John W . Krahn 2007-11-26, 10:02 pm |
| On Monday 26 November 2007 13:05, lerameur wrote:
> Hello,
Hello,
> I am trying to use this two line script. The command by itself works,
> when I run this script, I get error message:
> Use of uninitialized value in concatenation (.) or string at ./
> find_date.pl line 8.
>
> line 8: my $file_to_print = system "ls -lrt /test/*log | tail -1 |
> awk {'print $9'}";
> print $file_to_print;
>
> then after the error message an erroneous output, showing the correct
> file but with all the fields...
> how Do I get the script to function as if I where to implement the
> command directly.
my $dir = '/test';
opendir my $dh, $dir or die "Cannot open '$dir' $!";
my $file_to_print = (
map $_->[ 1 ],
sort { $a->[ 0 ] <=> $b->[ 0 ] }
map [ -M "$dir/$_", $_ ],
grep /log\z/,
readdir $dh
)[ 0 ];
John
--
use Perl;
program
fulfillment
| |
| Jenda Krynicky 2007-11-26, 10:02 pm |
| To: beginners@perl.org
From: lerameur <lerameur@yahoo.com>
Subject: error on simple system command
Date sent: Mon, 26 Nov 2007 13:05:53 -0800 (PST)
Organization: http://groups.google.com
> Hello,
>
> I am trying to use this two line script. The command by itself works,
> when I run this script, I get error message:
> Use of uninitialized value in concatenation (.) or string at ./
> find_date.pl line 8.
>
> line 8: my $file_to_print = system "ls -lrt /test/*log | tail -1 |
> awk {'print $9'}";
> print $file_to_print;
>
> then after the error message an erroneous output, showing the correct
> file but with all the fields...
> how Do I get the script to function as if I where to implement the
> command directly.
> Thanks,
There are at least two problems with the code.
1) The $9 is evaluated by perl, not passed as is to awk. You have to
escape $ by prepending a backslash if you want it to be treated
literally in a doublequoted string.
2)system() doesn't return the text printed by the program you run. It
returns "the exit status of the program as returned by the wait
call". That is, a number.
I think you wanted to use
`ls -lrt /test/*log | tail -1 | awk {'print \$9'}`
Please have a look at
perldoc -f system
and
perldoc perlop
(search for `STRING`)
Of course you should not be running external programs if all you want
is to find out the name of the last *log in a directory.
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Lerameur 2007-11-27, 10:01 pm |
| works good now:
my $file_to_print = ( `ls -1c /test/*log | tail -1 `);
print "file_to_print: $file_to_print";
open (FILE, "< /test/$file_to_print") or die "Could not open
file_to_print $: $!";
although the third line is not opening the file. It prints out good
but but I get an error while opening the file..
| |
| John W . Krahn 2007-11-27, 10:01 pm |
| On Tuesday 27 November 2007 08:18, lerameur wrote:
>
> works good now:
> my $file_to_print = ( `ls -1c /test/*log | tail -1 `);
>
> print "file_to_print: $file_to_print";
>
> open (FILE, "< /test/$file_to_print") or die "Could not open
> file_to_print $: $!";
>
> although the third line is not opening the file.
So if it "works good" what is the problem?
> It prints out good but but
How do you know that it "prints out good"?
> I get an error while opening the file..
What exact error message do you get?
Did you read the replies to your original message?
(Why do you have the $: variable in the error message?)
John
--
use Perl;
program
fulfillment
| |
| Tom Phoenix 2007-11-27, 10:01 pm |
| On 11/27/07, lerameur <lerameur@yahoo.com> wrote:
> works good now:
> my $file_to_print = ( `ls -1c /test/*log | tail -1 `);
>
> print "file_to_print: $file_to_print";
Since you didn't chomp() it, and since you didn't need to add a
newline when you printed it, it seems that $file_to_print still has a
trailing newline character. That's normal for commands run with
backticks, but it's not what shell programmers are used to. That's
why, if $file_to_print is supposed to be a filename, I'd probably
re-write that last line more like this:
print "file_to_print: '$file_to_print'\n";
> open (FILE, "< /test/$file_to_print") or die "Could not open
> file_to_print $: $!";
>
> although the third line is not opening the file. It prints out good
> but but I get an error while opening the file..
You probably didn't mean to use Perl's special $: variable in your
error message. But why don't you tell us what the error message was?
I'm guessing it was something like "file not found", since you don't
have a file whose filename ends in a newline character.
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|