Re: error on simple system command



To: beginners@xxxxxxxx
From: lerameur <lerameur@xxxxxxxxx>
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@xxxxxxxxxxx === 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

.



Relevant Pages

  • Re: Can perl do this?
    ... The system command will require a password, so the STDIN and STDOUT need to be played with so the password can be sent by the file * after the command, the script needs to monitor the folder again, and repeat after a change. ... Or can I do it in five minutes after I learned the language? ...
    (comp.lang.perl.misc)
  • Re: Cannot get exec syntax correct
    ... then run a system command against each file. ... > the script to fork off a process for each invocation, ... > # define SAS command string ...
    (comp.lang.perl.misc)
  • Re: How can I create symbol thru Perl "system" command?
    ... I have a simple script running perl 5 alpha vms 7.2, ... in creating any smbol thru perl system command. ... The symbol that you created only exists for the subprocess created by the system command. ...
    (comp.os.vms)
  • Cannot get exec syntax correct
    ... I'm trying to write a simple script that will glob all files with a ... then run a system command against each file. ... the script to fork off a process for each invocation, ...
    (comp.lang.perl.misc)
  • RE: array help
    ... I am executing following command. ... but the output of this command is not storing in the array. ... Let me look me in the manual of system command for the possible values ... I do perldoc perlop read the section under qx// and `STRING`. ...
    (perl.beginners)