delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/04/21:52:41

From: pjfarley AT dorsai DOT org (Peter J. Farley III)
Newsgroups: comp.os.msdos.djgpp
Subject: BASH (subshells), SHLVL not increasing?
Date: Wed, 04 Jun 1997 22:02:44 GMT
Organization: The Dorsai Embassy, Inc.
Lines: 112
Message-ID: <3395d9de.20512987@news.dorsai.org>
NNTP-Posting-Host: pjfarley.ppp1.dorsai.org
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

I have found what is either a very small or a very big bug in BASH,
I'm not sure which at this point.  For the record, here is the 'ls -l'
of my current version:

bash.exe//g/perl5.003$ ls -l /bin/bash.exe
-rwxr-xr-x   1 dosuser  dos        403968 Apr 19 22:03 h:/bin/bash.exe

I created a small set of test scripts that demonstrate the problem,
called testmem1, testmem2 and testmem3.  To reproduce the problem,
copy the three scripts to a working directory and run the first one as
'sh testmem1'.  I've appended the three scripts to the end of this
message so you can try them yourself.  Be aware, the first line in
testmem2 and in testmem3 is word-wrapped to two lines in this message,
each of them should be just one long line.

When they run, the first script will use '. ./testmem2' to start the
second script, which will then use (cd testdir && . ./testmem3) to
execute the third script (it first mkdir's testdir if it does not
exist and then cp's testmem3 to testdir).  The third script just does
a here-document cat to create a testfile in the testdir.  I display
the value of SHLVL at the start of each script.  When I run this under
BASH, the SHLVL variable never changes value; it is always 1.  I would
expect that SHLVL=2 in the "subshell" execution of the third script.
Here is a transcript of what it produces on my system:

G:\perl5.003>bshell testmem1
Now we are in the level 1 shell, SHLVL=1.
Switching source via '.' to secondary script.
In secondary script now, we should still be in the level 1 shell,
SHLVL=1.
Making test subdirectory and copying third script to it.
Executing another script in a sub-directory via subshell '()'.
In third script now, we should now be in the level 2 shell, SHLVL=1.
Finished cat ... '!GROK!THIS!'
Back to the secondary script, level 1 shell, SHLVL=1.
Leaving secondary script.
Back to the level 1 shell, SHLVL=1.
Leaving level 1.
G:\perl5.003>

Either the SHLVL shell variable is not being updated as subshells are
executed (small bug), or BASH does not really run a subshell, and so
does not respect subshell list constructions '(...)' (big bug?).  I am
afraid of the second possibility, especially if the variable
assignments and environment-affecting commands executed in the
subshell are not discarded on exit from the subshell.  This could
break many scripts, I believe.

I don't know if it is related, but I found this problem while trying
to track down an "Out of virtual memory" error I got while running the
Configure script from Perl5.003.  I have been working on this script
to get it to function correctly under DJGPP/BASH, with pretty good
success so far.  However, when it gets to the very *last* stage
(doesn't it figure? <g>), it runs a series of sub-scripts using the
syntax '. ./$file' (which should *not* update SHLVL, and indeed does
not do so), and then the final two are run as subshells in the form
(cd $dir && . ./$file), where $dir and $file have been set to an
existing directory and file, respectively.

The first of these (subshell) executions in a subdirectory fails with
an "Out of virtual memory" error, but ONLY if I run ALL of the
preceding sub-scripts.  I'm still experimenting with the number and
order of the preceding sub-scripts, to see if one particular
sub-script is causing the error to occur, but without any clues so
far.  In researching this problem, I found the SHLVL anomaly, so I
thought I'd report the SHLVL thing first in case it has any bearing on
the "Out of virtual memory" error.

Here are the test scripts to demonstrate the SHLVL problem:

----------------- testmem1 -------------------------
#! /bin/sh
echo "Now we are in the level 1 shell, SHLVL=$SHLVL."
echo "Switching source via '.' to secondary script."
. ./testmem2
echo "Back to the level 1 shell, SHLVL=$SHLVL."
echo "Leaving level 1."
exit

----------------- testmem2 -------------------------
echo "In secondary script now, we should still be in the level 1
shell, SHLVL=$SHLVL
echo "Making test subdirectory and copying third script to it."
if test ! -d ./testdir; then
        mkdir ./testdir
fi
cp testmem3 ./testdir/testmem3
echo "Executing another script in a sub-directory via subshell '()'."
(cd testdir && . ./testmem3)
echo "Back to the secondary script, level 1 shell, SHLVL=$SHLVL."
echo "Leaving secondary script."

----------------- testmem3 -------------------------
echo "In third script now, we should now be in the level 2 shell,
SHLVL=$SHLVL."
rm -f Testfile
cat >Testfile <<!GROK!THIS!
# $RCSfile: Testfile.SH,v $$Revision: 4.1 $$Date: 92/08/07 18:29:07 $
#
# $Log: Testfile.SH,v $

!GROK!THIS!

echo "Finished cat ... '!GROK!THIS!'"

----------------- end of test scripts --------------

TIA for any help or info you can provide on this.


----------------------------------------------------
Peter J. Farley III (pjfarley AT dorsai DOT org)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019