1N/A#
1N/A# @(#)dotsh.pl 03/19/94
1N/A#
1N/A# This library is no longer being maintained, and is included for backward
1N/A# compatibility with Perl 4 programs which may require it.
1N/A#
1N/A# In particular, this should not be used as an example of modern Perl
1N/A# programming techniques.
1N/A#
1N/A#
1N/A# Author: Charles Collins
1N/A#
1N/A# Description:
1N/A# This routine takes a shell script and 'dots' it into the current perl
1N/A# environment. This makes it possible to use existing system scripts
1N/A# to alter environment variables on the fly.
1N/A#
1N/A# Usage:
1N/A# &dotsh ('ShellScript', 'DependentVariable(s)');
1N/A#
1N/A# where
1N/A#
1N/A# 'ShellScript' is the full name of the shell script to be dotted
1N/A#
1N/A# 'DependentVariable(s)' is an optional list of shell variables in the
1N/A# form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
1N/A# dependent upon. These variables MUST be defined using shell syntax.
1N/A#
1N/A# Example:
1N/A# &dotsh ('/foo/bar', 'arg1');
1N/A# &dotsh ('/foo/bar');
1N/A# &dotsh ('/foo/bar arg1 ... argN');
1N/A#
1N/Asub dotsh {
1N/A local(@sh) = @_;
1N/A local($tmp,$key,$shell,$command,$args,$vars) = '';
1N/A local(*dotsh);
1N/A undef *dotsh;
1N/A $dotsh = shift(@sh);
1N/A @dotsh = split (/\s/, $dotsh);
1N/A $command = shift (@dotsh);
1N/A $args = join (" ", @dotsh);
1N/A $vars = join ("\n", @sh);
1N/A open (_SH_ENV, "$command") || die "Could not open $dotsh!\n";
1N/A chop($_ = <_SH_ENV>);
1N/A $shell = "$1 -c" if ($_ =~ /^\#\!\s*(\S+(\/sh|\/ksh|\/zsh|\/csh))\s*$/);
1N/A close (_SH_ENV);
1N/A if (!$shell) {
1N/A if ($ENV{'SHELL'} =~ /\/sh$|\/ksh$|\/zsh$|\/bash$|\/csh$/) {
1N/A $shell = "$ENV{'SHELL'} -c";
1N/A } else {
1N/A print "SHELL not recognized!\nUsing /bin/sh...\n";
1N/A $shell = "/bin/sh -c";
1N/A }
1N/A }
1N/A if (length($vars) > 0) {
1N/A open (_SH_ENV, "$shell \"$vars && . $command $args && set \" |") || die;
1N/A } else {
1N/A open (_SH_ENV, "$shell \". $command $args && set \" |") || die;
1N/A }
1N/A
1N/A while (<_SH_ENV>) {
1N/A chop;
1N/A m/^([^=]*)=(.*)/s;
1N/A $ENV{$1} = $2;
1N/A }
1N/A close (_SH_ENV);
1N/A
1N/A foreach $key (keys(%ENV)) {
1N/A $tmp .= "\$$key = \$ENV{'$key'};" if $key =~ /^[A-Za-z]\w*$/;
1N/A }
1N/A eval $tmp;
1N/A}
1N/A1;