[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3-unattended/var/se3/unattended/install/bin/ -> with-env.pl (source)

   1  # Run a command with an environment defined by another command.
   2  
   3  use warnings;
   4  use strict;
   5  use Getopt::Long;
   6  use Pod::Usage;
   7  
   8  # Your usual option-processing sludge.
   9  my %opts;
  10  GetOptions (\%opts, 'help', 'no-error')
  11      or pod2usage (2);
  12  
  13  (exists $opts{'help'})
  14      and pod2usage ('-exitstatus' => 0, -verbose => 2);
  15  
  16  # Ensure exactly two arguments
  17  scalar @ARGV == 2
  18      or pod2usage (2);
  19  
  20  my ($env_cmd, $cmd) = @ARGV;
  21  
  22  my @settings = `$env_cmd`;
  23  
  24  $opts{'no-error'} || $? == 0
  25      or die "$env_cmd failed, status ", $? / 256, '.', $? %256;
  26  
  27  # Update environment with settings.
  28  foreach my $setting (@settings) {
  29      chomp $setting;
  30      my ($var, $value) = $setting =~ /^([^=]+)=(.*)\z/;
  31      defined $var
  32          or die "Unable to parse output of $env_cmd:\n  $setting\n  ";
  33      $ENV{$var} = $value;
  34  }
  35  
  36  # Run "real" command.
  37  
  38  system $cmd;
  39  $? == 0
  40      or die "$cmd failed, status ", $? / 256, '.', $? %256;
  41  
  42  exit 0;
  43  
  44  __END__
  45  
  46  =head1 NAME
  47  
  48  with-env.pl - Use one command to set another's environment
  49  
  50  =head1 SYNOPSIS
  51  
  52  with-env.pl [ options ] <env_cmd> <cmd>
  53  
  54  Options (may be abbreviated):
  55  
  56   --help         Display this help and exit
  57   --no-error     Do not abort if <env_cmd> gives an error
  58  
  59  =head1 DESCRIPTION
  60  
  61  This script takes two arguments, which are both commands to be run.
  62  The first command should output one or more lines of the form:
  63  
  64    VARIABLE=VALUE
  65  
  66  (Note that there is no space around the equals sign.)  This script
  67  will add these settings to the environment, and then run the second
  68  command.


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1