Where $ PATH in the declared system ()

I had something strange on my computer. I had gperf installed in / usr / local / bin. Regarding the question I asked here , I had a perl script on my computer that contains the system () line on gperf with flags, something similar to

perl file:

system("gperf ...") == 0 || die "calling gperf failed: $?"; 

However, no matter how I try, gperf does not start and does not give an error message

for debugging I tried something like

system("echo \$PATH") == 0 || die "calling gperf failed: $?";

and found that it does not contain /usr/local/bin/ , where I installed my gperf, but look only in usr/bin , where it was not installed

So $PATH wrong ... So, I googled around and saw that system() matches the call to / bin / sh inside the file, so I tried /bin/sh and echo $PATH , which found that it contains /usr/local/bin/ for my invalid. So my question is: where is the $ PATH for the declared system ()? why is it different from the one inside the bourne shell?

+4
source share
2 answers

PATH used by the commands launched with system is the same as in the perl script, accessible via $ENV{PATH} . This is the PATH that the perl script inherits from the program being called, unless you change it in the script.

What will bite you is probably that you configured your PATH in the wrong configuration file. Define it in ~/.profile , /etc/profile or another system-wide file, and not in a shell configuration file, such as .bashrc . See this question for some general information.

If you want to set the path manually inside the perl script, you can use something like

 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}" unless ":$ENV{PATH}:" =~ m~:/usr/local/bin:~; 

but this is probably a bad idea: in most cases, your script should not change the path chosen by the user who runs this script.

If you have problems finding the right place to install PATH on your system after reading the question I'm connected with and the questions related to my answer, ask Unix and Linux and do not forget to specify the details of your operating system (distribution, version, etc.) .d.) and how you enter the system (this is a user question, not a programming question).

+7
source

On a linux system that uses BASH as a shell, PATH is set during login from the user’s .bash_profile file in its home directory. You can add the directory / usr / local / bin with a line like this at the end of the file:

  PATH=$PATH:/usr/local/bin 

Another (possibly more reliable) way to fix this is to use the absolute path in the system call, for example:

  system("/usr/local/bin/gperf") 
0
source

All Articles