1

I'm having a tough time figuring out why the following command is not being executed in perl..

system(". ./setenv.sh");

I've tried alternatives such as

ALT 1 = system ".", "./setenv.sh";

ALT 2 = `. ./setenv.sh`;

Not sure what I'm doing wrong.. Any ideas?

EDIT: I figured it out with some of the ideas mentioned here, this is what I did..

system(". ./setenv.sh && <other commands that I required the env for here>");

Thanks!

simar
  • 23
  • 5
  • 1
    What kind of symptoms are you seeing, i.e., how do you know it is not being executed? – Eric Mar 14 '12 at 17:30
  • 1
    What is the point of the first . ? – kclair Mar 14 '12 at 17:31
  • Is it possible for the current directory `.` to be an executable command? What are you trying to do? – Zaid Mar 14 '12 at 17:31
  • 2
    It is being executed, but it's not doing anything useful, because environment variables propagate downwards through the process tree, not upwards. – hobbs Mar 14 '12 at 19:38

3 Answers3

7

. and source (equivalents) are bash internal commands. The system call tries to find the executable (the first bit before the space) and does not find it (. does not exist as executable as it is a bash internal command).

You have to execute bash (or the shell you're using) directly using system:

system ("/bin/bash ./setenv.sh");

However, note that what you're trying to do, unless setenv.sh has some side effects, have no effect, as the shell you start to read that environment dies just after executing the system line. To establish the environment for your perl program you should be using the %ENV variable.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
3

Judging by the name of the script, setenv.sh, it looks like you might be expecting that certain environment variables will be set after the execution of your Perl script.

However, your shell script does not have access to the parent environment, and any environment changes it makes are lost when the script exits.

Is the lack of changed environment variables your reasoning for believing that your shell script is not being executed?

For more information, see Can a shell script set environment variables of the calling shell?

Community
  • 1
  • 1
Eric
  • 5,842
  • 7
  • 42
  • 71
2

The . is a shell built-in (also known as source). The system function does not feed the arguments through a shell, at least not in the basic case. That means you are trying to execute a regular command called ., and there is none such.

Interestingly, Perl will feed the system arguments through shell when it notices some shell metacharacters inside, so system(". setenv.sh | cat") will work, for example.

zoul
  • 102,279
  • 44
  • 260
  • 354