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

Hi, I can run shell script from the command line using
$ . set
If the run the script inside perl script using
system("set");
The code seems like it doesnot execute the line.

Replies are listed 'Best First'.
Re: Run shell script in perl
by ikegami (Patriarch) on Feb 26, 2009 at 06:26 UTC

    You have a problem and you don't even check what error you got? See the docs for how to find out.

    . set means "execute the commands in file 'set' in the current process".

    system("set") means "execute program or script 'set'".

    There are differences. Most relevant is probably the lack of requirement for set to be executable in the former.

    By the way, set is an awful name because a sh/bash/... command has that same name.

      it is the executable shell script the file name given as $ . set If I change the shell script file name how can I use from perl script code
        You still haven't told us the error returned by system. I'm not playing the guessing game when the information is readily available.
Re: Run shell script in perl
by cdarke (Prior) on Feb 26, 2009 at 08:55 UTC
    A ikegami said, . and system are different, but it looks like you do not appreciate what either do.

    The shell 'dot' command . (also known as 'source' in Bash and C shell) asks the current shell to execute the shell statements within the specified file. There are ways to do the same thing with Perl, but only with Perl statements (although Perl does allow inline statements from languages like C and Java). You only need read access on the file being used, since the shell is reading the statements as if you were typing them in. The most common reason for using the 'dot' command is to set environment variables in the current shell. The syntax for doing this in shell and Perl are very different, so even if you did do this in Perl it wouldn't work.

    When you run a script from a shell, for example by typing ./script_name you will see that you need execute access on the script because it will run in another process. That is also what system does, and yours might be failing because of permissions (try chmod u+x script_name), or because the file is not in a directory which is in the PATH environment variable (echo $PATH, or specifiy the current directory). Any environment variables set in this way will only affect the new process - child processes run with a sort of filewall around them so they do not accidentally clobber anyone else.

    You do not say what your error message is, nor do you say why you are executing the 'dot' command. If you did then we might be able to help further.
Re: Run shell script in perl
by helgi (Hermit) on Feb 26, 2009 at 08:43 UTC
    Try
    system ("/full/path/to/set") == 0 or die "Cannot run set:$?";
    I.e. if the error code from set is anything but zero, stop running and show me the external program error code ($?). Use the full path to make sure that you're running the right thing. Also, the current directory may or may not be in your path.

    --
    Regards,
    Helgi Briem
    hbriem AT f-prot DOT com
Re: Run shell script in perl
by Bloodnok (Vicar) on Feb 26, 2009 at 11:47 UTC
    At the risk of repeating what's been said elsewhere, the 2 statements are not equivalent since the former uses the commands defined in set (as ikegami rightly says, that [set] is a spectacularly bad choice of name for a command script since it may well lead to all sorts of other problems) to update the environment for the duration of the process, whereas the latter attempts to run set as a shell script in a sub-process.

    The equivalent of

    $ . set $ stuff $ requiring $ modified $ environment
    would be
    system '. set ; stuff requiring modified environment' == 0 or die "sys +tem() failed - $!";

    A user level that continues to overstate my experience :-))
Re: Run shell script in perl
by zentara (Cardinal) on Feb 26, 2009 at 14:04 UTC
    In my view of things, this is how it is. :-) If you do "which -a set", you will not get any program named set, that is because it is a built-in bash shell command. So.... use the -c option of bash (read man bash)
    #!/usr/bin/perl use warnings; use strict; my @output = `bash -c set`; print @output,"\n";

    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Run shell script in perl
by Corion (Patriarch) on Feb 26, 2009 at 17:50 UTC