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

hi monks, I have some problems. for some reason, I can't execute system ("echo HELLO WORLD; &"); I know this command executes when i put it into the unix terminal. I also know that it will work if i take out the semi colon. However, I need to execute commands as if it was used for the terminal. I want to execute commands and directly continue through the script. (the commands could take 5- 10 mins to execute).

Replies are listed 'Best First'.
Re: executing commands and going on
by davidrw (Prior) on May 13, 2005 at 16:05 UTC
    I'm curious if system("nohup echo HELLO WORLD &") would work (btw, is that semi-colon really supposed to be before the ampersamp?) but what you're really looking for is fork() (perldoc -f fork). If you need to do more stuff in the original process when the long commands finally finish, that's a different issue...
Re: executing commands and going on
by polettix (Vicar) on May 13, 2005 at 16:16 UTC
    If you want to print
    HELLO WORLD;
    you have to escape the ";" character, which is interpreted as command separator by the shell:
    system ("echo HELLO WORLD\\; &");
    Note the double backslash: you're using single quotes, so the backslash is the escaping character. The first escapes the second, which gets passed to the shell to escape the ";". There are almost infinite variations in your quoting approach, both within Perl and the shell, but it's left as an exercise :)

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
      system(qq{echo "HELLO WORLD;" &});

      also does the trick.

Re: executing commands and going on
by Belgarion (Chaplain) on May 13, 2005 at 16:05 UTC

    Are you sure the command actually excutes at the shell? If I enter echo HELLO WORLD; & at the shell (bash in this case), I get the following error: bash: syntax error near unexpected token `&'

Re: executing commands and going on
by scmason (Monk) on May 13, 2005 at 16:15 UTC
    Belgarion is right, this is not valid syntax. But what you really want to do is fork and execute the command in a sub-process. Like this:
    $pid = fork; if ($pid) { #This is the parent, continue processing } else { #this is the child, so exec #which will replace the child exec("echo HELLO WORLD"); }
Re: executing commands and going on
by bart (Canon) on May 13, 2005 at 18:29 UTC
    It typically sounds like the problem that echo isn't actually a program, but an internal command from the shell. In that way, it's a FAQ.

    Try running something that actually has an associated program — or run a copy from the shell telling it to echo some text.