On May 26, 2010, at 9:03 AM, Andros Zuna wrote:

> Hi,
> I did some few changes:
> 
> #!
> /usr/bin/perl
> 
> # use
> warnings;
> # use
> strict;
> 
> 
> $command = `ls
> -l`;
> 

Looks like you're using backticks. Backticks actually causes perl to execute 
the command inside them and return the output. So in this line you are 
executing 'ls -l' and assigning the output of that command to $command. You're 
them subsequently passing that output into the system() call which is causing 
system() to complain. 

You should just use regular quotes instead of backticks.

Also, when dealing with external commands like this, it is usually a good idea 
to use the full path for everything. So i would alter the line like so:

$command = "/bin/ls -l /full/path/to/directory/we/are/listing/";

> 
> if (system($command) == 0)
> {
> }
> 
> elsif(system($command) != 0)
> {
>    print "Failed to execute:
> $?\n";
>    my
> $c++
> }

Couple things here. You don't need an 'elsif'. All you need is 'else'. No point 
in executing the command a second time since we already know it doesn't equal 
zero if the second block of code is executing.

But I'm not sure why you are using an if statement at all, since you mentioned 
earlier you want to continue to execute a command until you get the desired 
output. If statements only evaluate a condition once. If you want to check it 
more than once, you need a loop. Someone else already posted the answer to this:

while (system($command) != 0) {
}


> 
> But I'm still not getting it right, this is my error output:
> 
> root@/user$ ./script.pl
> sh: total: command not found
> sh: line 1: -rw-r--r--: command not found
> sh: line 2: -rw-r--r--: command not found
> sh: line 3: -rwxr-xr-x: command not found
> sh: line 4: -rwxr-xr-x: command not found
> sh: line 5: -rwxr-xr-x: command not found
> sh: line 6: -rwxr-xr-x: command not found
> sh: line 7: -rwxr-xr-x: command not found
> sh: line 8: -rwxr-xr-x: command not found
> sh: line 9: -rwxr-xr-x: command not found
> sh: line 10: -rwxr-xr-x: command not found
> sh: total: command not found
> sh: line 1: -rw-r--r--: command not found
> sh: line 2: -rw-r--r--: command not found
> sh: line 3: -rwxr-xr-x: command not found
> sh: line 4: -rwxr-xr-x: command not found
> sh: line 5: -rwxr-xr-x: command not found
> sh: line 6: -rwxr-xr-x: command not found
> sh: line 7: -rwxr-xr-x: command not found
> sh: line 8: -rwxr-xr-x: command not found
> sh: line 9: -rwxr-xr-x: command not found
> sh: line 10: -rwxr-xr-x: command not found
> Failed to execute: 32512

This output is because you are passing a directory listing to a system() call 
(because you used backticks earlier).



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to