in reply to Calling a shell script after changing directory

Hi Gouravhere,

You could use chdir to change the directory before executing the script, or perhaps File::pushd to easily restore the original working directory.

However, are you sure your script is failing? Have a look at the documentation of system for its return value and error handling: the convention is that an exit code of zero means success, so if your test1.sh follows this convention (which I'd strongly recommend), then the correct way to check for errors is system(@args) == 0 or die ....

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Calling a shell script after changing directory
by Gouravhere (Initiate) on May 11, 2016 at 07:05 UTC
    Hi Hauke, Thanks for your reply. I can see that the script is failing. I am actually executing a shell script which sets environment variables. Now after executing the script I cant see the variables. Also you can see that the process is getting halted as it encounters  die which I kept right after the system call. Regards, Gourav
      I am actually executing a shell script which sets environment variables

      That is not going to work. The scope of environment variables is not global but limited to the process where they are set and subprocesses forked from it which inherit them.

      In your code, system launches a shell that starts a new shell for executing test1.sh. Any environment variable set in the script just lives until the second shell terminates.

      A possible way to work around that is to dump the variables from the shell before it terminates. For instance:

      # untested; my $set_env = `cd $dir && source test1.sh && perl -MData::Dumper -e 'p +rint Dump \\\%ENV'` and die; print $set_env;

        Thanks Salva. there seems to be an issue with the shell script which is setting the environment variables. I am calling a shell file siebenv.sh which resides in the siebel server. if you want to execute it manually then you just need to fire the following command.

         . ./siebenv.sh Now when you do that from perl, it is not happening. I guess there is nothing to do from Perl. But still I am lost here :)