redjac has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to run the following command. system "nzbget $item"; Nzbget is an executable file, which works fine from the command line, but the script cannot seem to run. $item is a filename I am passing it. Does anyone have any ideas why I can run it manually, but the script cannot? Thank you, Trent

Replies are listed 'Best First'.
Re: "System" will not run an executable
by serf (Chaplain) on Dec 04, 2005 at 03:30 UTC
    Firstly find the program that you're running and provide the full path to it.

    If you're in +n.x you can find this with:

    which nzbget

    let's say it tells you it's in /usr/local/bin/nzbget you would modify your code to be:

    my @args = ("/usr/local/bin/nzbget", $item); system(@args) == 0 or die "Could not run '@args' ($?): $!\n";
    The $! is *very* useful to help you here because it will tell you the system error that is being thrown to tell you why the command is failing.

    The ($?) will show you the return code that the program failed with (in case there was nothing useful in $!).

    Watch out for the fact that system is a little different from other functions that you expect to return 1 on success such as open:

    my $args = "/usr/local/bin/nzbget $item" open("$args |") || die "Can't run '$args': $!\n";
    If you read system you will see that this is based around the fact that a program exiting successfully normally returns 0 (and system() does special magic on the return code to give you more useful info wrapped up in one number)
      If you're in +n.x

      I don't think he is. Did you read his post closely? He doesn't actually say it, but it's pretty obvious he's on Windows. First, he changes the case on the name of his executable from one usage to the next, betraying an assumption that the filesystem is not case-sensitive, which pretty much precludes any POSIX system. His title also has System in title case, further evidence he's not thinking like a *nix user.

      Second, the wording where he says that the file is executable strongly reminds me of the way DOS and Windows users state such things ("is an executable file"), not the way *nix people usually say it ("file is executable by the user" or "file has execute permission" or such). This is perhaps subtle, but the wording makes me think, "the filename has .exe on the end of it", not "chmod +x".

Re: "System" will not run an executable
by ikegami (Patriarch) on Dec 04, 2005 at 03:37 UTC

    I believe you mentioned in the CB that you solved this problem. ("sorry, I just figured it out. The variable is not being passed properly.") I bet it's because you didn't properly quote and escape the contents of $item. I suggest that you use substitute
    system("nzbget $item")
    with
    system('nzbget', $item)
    It's safer since a shell isn't loaded, and you don't have to escape the contents of $item.