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

#!/usr/bin/perl -w use strict; use warnings; my $home='~/bin/MIReNA-2.0'; exec("cd $home"); exec("./MIReNA.sh");
I am trying to run a shell script MIReNA.sh from within perl. could not run properly.

Replies are listed 'Best First'.
Re: could not get exec and sytem to work
by SuicideJunkie (Vicar) on Aug 19, 2014 at 19:59 UTC

    Also notable, is that you don't want to create a child, have the child change its current directory, and then kill the child. That has no effect on your own current directory.

    See chdir instead.

Re: could not get exec and sytem to work
by McA (Priest) on Aug 19, 2014 at 18:50 UTC

    Hi,

    let me cite the documentation to exec:

    The exec function executes a system command and never returns ...

    This should explain what you get. You never get to your second exec line.

    Regards
    McA

Re: could not get exec and sytem to work
by Laurent_R (Canon) on Aug 19, 2014 at 20:50 UTC
    The best solution is to use the already mentioned chdir Perl built-in to get into the right default directory. But if you want to do it in shell, you could issue two shell commands within the same exec or system shell command string, for example;
    my $home='~/bin/MIReNA-2.0'; system("cd $home; ./MIReNA.sh");
    An example Perl one-liner on my system:
    $ perl -e ' exec "cd ./Matt; ls -l"' total 11756 drwxr-xr-x+ 1 Laurent None 0 27 juin 19:26 appartement -rwxr-xr-x 1 Laurent None 12038144 27 juin 22:27 appartement.tar
    In the latest example above, I used exec because I did not care that the program would die immediately after having displayed the content of the directory. I would use system if I wanted the Perl program to do other things after having run the shell command. And if I wanted to retrieve the data in Perl (which is probably the most common use of shelling out calls to the OS), say to format the output of the ls command with line numbers, then I would use backticks instead of exec or system:
    $ perl -E ' my @content = `cd ./Matt; ls -l`; print ++$i, ": $_" for +@content' 1: total 11756 2: drwxr-xr-x+ 1 Laurent None 0 27 juin 19:26 appartement 3: -rwxr-xr-x 1 Laurent None 12038144 27 juin 22:27 appartement.tar
Re: could not get exec and sytem to work
by Anonymous Monk on Aug 19, 2014 at 20:31 UTC

    Your post title says, "could not get exec and sytem to work", but your code doesn't show any use of system. Could you show the code that fails with system, and provide the error messages? "could not run properly" is not enough for us to help you. Please see How do I post a question effectively?.