To: beginners@perl.org From: lerameur <[EMAIL PROTECTED]> 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 ===== [EMAIL PROTECTED] === 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/