2 # @(#)dotsh.pl 03/19/94
4 # This library is no longer being maintained, and is included for backward
5 # compatibility with Perl 4 programs which may require it.
7 # In particular, this should not be used as an example of modern Perl
8 # programming techniques.
11 # Author: Charles Collins
14 # This routine takes a shell script and 'dots' it into the current perl
15 # environment. This makes it possible to use existing system scripts
16 # to alter environment variables on the fly.
19 # &dotsh ('ShellScript', 'DependentVariable(s)');
23 # 'ShellScript' is the full name of the shell script to be dotted
25 # 'DependentVariable(s)' is an optional list of shell variables in the
26 # form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
27 # dependent upon. These variables MUST be defined using shell syntax.
30 # &dotsh ('/tmp/foo', 'arg1');
31 # &dotsh ('/tmp/foo');
32 # &dotsh ('/tmp/foo arg1 ... argN');
36 local($tmp,$key,$shell,$command,$args,$vars) = '';
40 @dotsh = split (/\s/, $dotsh);
41 $command = shift (@dotsh);
42 $args = join (" ", @dotsh);
43 $vars = join ("\n", @sh);
44 open (_SH_ENV
, "$command") || die "Could not open $dotsh!\n";
46 $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh
|\
/ksh|\/zsh
|\
/csh))\s*$/);
49 if ($ENV{'SHELL'} =~ /\/sh
$|\
/ksh$|\/zsh
$|\
/bash$|\/csh
$/) {
50 $shell = "$ENV{'SHELL'} -c";
52 print "SHELL not recognized!\nUsing /bin/sh...\n";
53 $shell = "/bin/sh -c";
56 if (length($vars) > 0) {
57 system "$shell \"$vars;. $command $args; set > /tmp/_sh_env$$\"";
59 system "$shell \". $command $args; set > /tmp/_sh_env$$\"";
62 open (_SH_ENV
, "/tmp/_sh_env$$") || die "Could not open /tmp/_sh_env$$!\n";
69 system "rm -f /tmp/_sh_env$$";
71 foreach $key (keys(%ENV)) {
72 $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;