Last change
on this file since 31993 was 29689, checked in by Jeremy Symon, 9 years ago |
Example of reliably sourcing a shell script for environment variables from within perl
|
-
Property svn:executable
set to
*
|
File size:
667 bytes
|
Line | |
---|
1 | #!/usr/bin/env perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use utf8;
|
---|
6 |
|
---|
7 | my $command =
|
---|
8 | # Source the setup script to get environment variables.
|
---|
9 | # Ensure that no output is printed.
|
---|
10 | '. ./setup.sh >/dev/null 2>/dev/null;' .
|
---|
11 | # While in the environment which has the new variables,
|
---|
12 | # Use a perl command to print the environment variables as valid perl code
|
---|
13 | 'perl -e \'for (keys %ENV) { print "\$ENV{\'\\\'\'", $_, "\'\\\'\'} = \'\\\'\'", $ENV{$_}, "\'\\\'\';\n"; }\'';
|
---|
14 | # Evaluate the perl code to set the variables in our own environment
|
---|
15 | eval `$command`;
|
---|
16 | # Print out the environment variables (as an example to show it works)
|
---|
17 | for (keys %ENV) {
|
---|
18 | print $_, ' = ', "'$ENV{$_}'", "\n";
|
---|
19 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.