Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer this question properly, I need to know a bit more.</p> <ul> <li>Is it okay to actually run the shell script from within the perl script?</li> <li>Are the variable assignments all of the form <code>export VAR=value</code> (i.e. with fixed assignments, no variable substitutions or command substitutions)?</li> <li>Does the shell script do anything else but assign variables?</li> </ul> <p>Depending on answers to these, options of different complexity exist.</p> <p>Thanks for the clarification. Okay, here's how to do it. Other than assigning variables, your script has no side effects. This allows to run the script from within perl. How do we know what variables are exported in the script? We could try to parse the shell script, but that's not the Unix way of using tools that do one thing well and chain them together. Instead we use the shell's <code>export -p</code> command to have it announce all exported variables and their values. In order to find <em>only</em> the variables actually set by the script, and not all the other noise, the script is started with a clean environment using <code>env -i</code>, another underestimated POSIX gem.</p> <p>Putting it all together:</p> <pre><code>#!/usr/bin/env perl use strict; use warnings; my @cmd = ( "env", "-i", "PATH=$ENV{PATH}", "sh", "-c", ". ./myshell.sh; export -p" ); open (my $SCRIPT, '-|', @cmd) or die; while (&lt;$SCRIPT&gt;) { next unless /^export ([^=]*)=(.*)/; print "\$ENV{$1} = '$2'\n"; $ENV{$1} = $2; } close $SCRIPT; </code></pre> <p>Notes:</p> <ul> <li>You need to pass to <code>env -i</code> all environment your <code>myshell.sh</code> needs, e.g. <code>PATH</code>.</li> <li>Shells will usually export the <code>PWD</code> variable; if you don't want this in your perl ENV hash, add <code>next if $1 eq 'PWD';</code> after the first <code>next</code>.</li> </ul> <p>This should do the trick. Let me know if it works.</p> <p>See also:</p> <ul> <li><a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/export.html" rel="nofollow">http://pubs.opengroup.org/onlinepubs/009695399/utilities/export.html</a></li> <li><a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/env.html" rel="nofollow">http://pubs.opengroup.org/onlinepubs/009695399/utilities/env.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload