2 # @(#)dotsh.pl 03/19/94
4 # Author: Charles Collins
7 # This routine takes a shell script and 'dots' it into the current perl
8 # environment. This makes it possible to use existing system scripts
9 # to alter environment variables on the fly.
12 # &dotsh ('ShellScript', 'DependentVariable(s)');
16 # 'ShellScript' is the full name of the shell script to be dotted
18 # 'DependentVariable(s)' is an optional list of shell variables in the
19 # form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
20 # dependent upon. These variables MUST be defined using shell syntax.
23 # &dotsh ('/tmp/foo', 'arg1');
24 # &dotsh ('/tmp/foo');
25 # &dotsh ('/tmp/foo arg1 ... argN');
29 local($tmp,$key,$shell,$command,$args,$vars) = '';
33 @dotsh = split (/\s/, $dotsh);
34 $command = shift (@dotsh);
35 $args = join (" ", @dotsh);
36 $vars = join ("\n", @sh);
37 open (_SH_ENV, "$command") || die "Could not open $dotsh!\n";
39 $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh|\/ksh|\/zsh|\/csh))\s*$/);
42 if ($ENV{'SHELL'} =~ /\/sh$|\/ksh$|\/zsh$|\/bash$|\/csh$/) {
43 $shell = "$ENV{'SHELL'} -c";
45 print "SHELL not recognized!\nUsing /bin/sh...\n";
46 $shell = "/bin/sh -c";
49 if (length($vars) > 0) {
50 system "$shell \"$vars;. $command $args; set > /tmp/_sh_env$$\"";
52 system "$shell \". $command $args; set > /tmp/_sh_env$$\"";
55 open (_SH_ENV, "/tmp/_sh_env$$") || die "Could not open /tmp/_sh_env$$!\n";
62 system "rm -f /tmp/_sh_env$$";
64 foreach $key (keys(%ENV)) {
65 $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;