0

I have a shell script to change my path permanently to another path. My requirement is to source it using a perl script(perl script has some other code as well and finally it has to source the below shell scipt)

test.sh:

!/bin/bash
cd /db/oa/invoke/path

test.pl:

!/usr/bin/perl
system("source /home/test.sh");

but sourcing is not working. I know the command system("sh /home/test.sh --help") for shell script execution from perl script. But I need similar command to source the shell script. Please let know, if I am missing any details. I have already seen questions being asked on same topic, but i didn't see a proper solution for sourcing. can anyone help me on this.

Regards Anand

Jens
  • 67,715
  • 15
  • 98
  • 113
  • There is no sane way to do what you are asking. A subprocess cannot affect its parent's environment. You would basically have to have Perl `exec` Bash and then have that `exec` Perl back over itself in order to pull this off. – tripleee Mar 17 '17 at 07:43
  • Also the shebang needs to start with the two characters `#!` in order for it to be syntactically valid and actually useful. – tripleee Mar 17 '17 at 07:46
  • 1
    If you're shell script, as in your example, is only changing directory you can use perl's `chdir` function to change directory. You still have all the issues that have been raised above though. – mttrb Mar 17 '17 at 08:52

1 Answers1

0

As @tripleee said, you must exec yourself. Here is how I would do it:

if (defined $ENV{TOTO}) {
    print "TOTO is $ENV{TOTO}\n";
} else {
    my $cmd = ". ./source.txt ; exec $^X $0 " . join(' ', @ARGV);
    system ($cmd);
}

my script source.txt contains

export TOTO=titi
BOC
  • 1,109
  • 10
  • 20