cntr=1
while read invr
do
eval arry"$cntr"'="$invr"'
cntr=`expr $cntr + 1`
done < myfile
echo $arry1
The basic idea is that the script reads lines from myfile into
simulated array variables arry1, arry2, arry3, and so on... Then, to
test whether the lines were read into variables correctly, the value
of the first variable (that is, the value of arry1) is echoed to
standard output.
Unfortunately, this simple script does not seem to work on all Bourne
shells. For example, let myfile contain the following three lines:
one
two
three!
and let the script be contained in the file test.sh. Then, using
SunOS 5.8, I get the following result from running the script:
% sh test.sh
%
Apparently, the variable arry1 is empty! But the script works as
intended from the Korn shell
% ksh test.sh
one
%
and also works from the Bourne shell in the version of BSD Unix
included in MacOS X.
So, what is the problem? Is there a fix that is compatible will all
Bourne shells (since Unix Version 7)?
> Unfortunately, this simple script does not seem to work on all Bourne
> shells. For example, let myfile contain the following three lines:
>
> one
> two
> three!
>
> and let the script be contained in the file test.sh. Then, using
> SunOS 5.8, I get the following result from running the script:
>
> % sh test.sh
>
> %
>
> Apparently, the variable arry1 is empty! But the script works as
> intended from the Korn shell
>
> % ksh test.sh
As the name implies, ksh != sh (except under POSIX, confusingly).
> and also works from the Bourne shell in the version of BSD Unix
> included in MacOS X.
MacOS is porbably using the POSIX version of sh, which is basically
the same as ksh.
> So, what is the problem? Is there a fix that is compatible will all
> Bourne shells (since Unix Version 7)?
If you stick to the (pre-POSIX) sh stuff, that should be portable.
--
Rich Teer
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
POSIX allows both variants (subshell or not) in this case.
THAT's the confusing point.
ciao
--
"Unix was the first OS where you could carry the media and system
documentation around in a briefcase. This was fixed in BSD4.2."
> I have the following Bourne shell script
> cntr=1
> while read invr
> do
> eval arry"$cntr"'="$invr"'
> cntr=`expr $cntr + 1`
> done < myfile
> echo $arry1
If you intend a Bourne shell to run this program, then your
program has a bug.
$ man sh
. . .
Because commands in pipelines are run as separate processes,
variables set in a pipeline have no effect on the parent
shell.
. . .
$
The input redirection causes the while loop to be executed in
a subshell, so any variable you set in the while loop will not
be visible outside the while loop.
> Unfortunately, this simple script does not seem to work on all Bourne
> shells.
It is not going to work in any real Bourne shell. It will work in
POSIX shells pretending to be a Bourne shell. Many Unix and Unix-like
systems doesn't have real Bourne shells today, instead they use a
/bin/sh that is linked to ksh, pdksh, bash, ash, etc.
> So, what is the problem? Is there a fix that is compatible will all
> Bourne shells (since Unix Version 7)?
Yes, this corrected version of your program executes as expected.
cntr=1
exec 9<&0
exec 0< myfile
while read invr; do
eval arry"$cntr"'="$invr"'
cntr=`expr $cntr + 1`
done
exec 0<&9
exec 9<&-
echo $arry1
--
Göran Larsson http://www.mitt-eget.com
[while loop with redirected input]
Philipp> POSIX allows both variants (subshell or not) in this case.
I don't believe it does (please cite specific language if you
disagree). If the input is coming from a _pipe_, then POSIX does
indeed allow either variant, because the last command in a pipeline
may or may not be executed in a subshell environment. However I see
nothing that permits a loop to be executed in a subshell command
simply because a redirection was used.
--
Andrew.
comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
or <URL: http://www.whitefang.com/unix/>
I stand corrected, I was not reading carefully enough, since
I've seen this (with pipe) so very often :>
On HPUX we have found subtle differences between the Korn shell and the Posix shell
(and between different versions of the Korn shell).
--
Stephen Baynes CEng MBCS Stephen...@soton.sc.philips.com
Philips Semiconductors Ltd
Southampton SO15 0DJ +44 (0)23 80316431
United Kingdom My views are my own.