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

3
  • 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. Commented Mar 17, 2017 at 7:43
  • Also the shebang needs to start with the two characters #! in order for it to be syntactically valid and actually useful. Commented Mar 17, 2017 at 7: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. Commented Mar 17, 2017 at 8:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.