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,*dotsh,$command,$args,$vars) = '';
31 @dotsh = split (/\s/, $dotsh);
32 $command = shift (@dotsh);
33 $args = join (" ", @dotsh);
34 $vars = join ("\n", @sh);
35 open (_SH_ENV, "$command") || die "Could not open $dotsh!\n";
37 $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh|\/ksh|\/zsh|\/csh))\s*$/);
40 if ($ENV{'SHELL'} =~ /\/sh$|\/ksh$|\/zsh$|\/csh$/) {
41 $shell = "$ENV{'SHELL'} -c";
43 print "SHELL not recognized!\nUsing /bin/sh...\n";
44 $shell = "/bin/sh -c";
47 if (length($vars) > 0) {
48 system "$shell \"$vars;. $command $args; set > /tmp/_sh_env$$\"";
50 system "$shell \". $command $args; set > /tmp/_sh_env$$\"";
53 open (_SH_ENV, "/tmp/_sh_env$$") || die "Could not open /tmp/_sh_env$$!\n";
60 system "rm -f /tmp/_sh_env$$";
62 foreach $key (keys(%ENV)) {
63 $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;