[prog] Shell question...
Riccarda Cassini
riccarda.cassini at gmx.de
Fri Dec 10 12:32:58 EST 2004
On Fri, Dec 10, 2004 at 09:14:42AM +1100, John Clarke wrote:
> On Thu, Dec 09, 2004 at 10:13:58 +0100, Riccarda Cassini wrote:
>
> > echo bla blah blurb | read FOO BAR BAZ
>
> Try this instead:
>
> echo "bla blah blurb" | while read FOO BAR BAZ
> do
> echo $FOO
> echo $BAR
> echo $BAZ
> done
> ... Putting the read in a while loop makes the variables available
> within the loop, because the entire loop is run in the child process.
Thanks, John! This does work, in principle...
Problem is, I'd rather have those variables available in the main shell
(the parent process), because many of them are subsequently being used
in a multitude of places all over the script.
If I understand things correctly, I'd have to do something like the
following, in order to get this working
echo bla blah blurb | while read FOO BAR BAZ
do
echo $FOO
echo $BAR
echo $BAZ
echo bla-$FOO blah-$BAR blurb-$BAZ | while read FOO2 BAR2 BAZ2
do
echo $FOO2
echo $BAR2
echo $BAZ2
echo bla-$FOO2 blah-$BAR2 blurb-$BAZ2 | while read FOO3 BAR3 BAZ3
do
echo $FOO3
echo $BAR3
echo $BAZ3
done
done
done
This appears to work as expected - at least it prints
$ ./test.ksh
bla
blah
blurb
bla-bla
blah-blah
blurb-blurb
bla-bla-bla
blah-blah-blah
blurb-blurb-blurb
but :-) this not only doesn't exactly make the code more readable, it
also keeps all those child processes running (well, spinning, more
precisely) until the very end of the script.
If I put a 'ps axf' in the innermost child loop, in can see that there
were in fact multiple child processes being spawned:
15577 ttyp0 S 0:00 | \_ /usr/bin/ksh ./test.ksh
15579 ttyp0 S 0:00 | \_ /usr/bin/ksh ./test.ksh
15581 ttyp0 S 0:00 | \_ /usr/bin/ksh ./test.ksh
15583 ttyp0 S 0:00 | \_ /usr/bin/ksh ./test.ksh
15584 ttyp0 R 0:00 | \_ ps axf
In my real-world scenario this would then need to go up to a nesting
level of about 20..30 (!), per script. Not to mention all those other
greps, awks, etc... I guess I'll better keep mem-usage per child as
low as possible... ;-) Or, can anyone recommend some other approach?
> > P.S.: does anyone know how to find out which ksh versions I have?
> > I tried 'ksh -v', 'ksh --version', 'ksh -h', but all to no avail.
> > Also, there doesn't seem to be a respective shell variable I might
>
> There is:
>
> [johnc at dropbear ~]$ ksh
> $ echo $KSH_VERSION
> @(#)PD KSH v5.2.14 99/07/13.2
>
> KSH_VERSION is listed in the man page :-)
yes, you're right, this works for PD KSH - but not for the ksh I have
on HP-UX. There, I simply get
$ ksh
$ echo $KSH_VERSION
$
but from that I can at least infer that it's not the PD KSH :-)
Actually, I kept being curious, and finally did find
^V Display version of the shell.
being mentioned in the man page section "Emacs Editing Mode".
So,
$ ksh
$ EDITOR=emacs
(now press ^V, i.e. Ctrl-V, twice!(?) )
$ Version 11/16/88
...prints the version.
Kinda cumbersome, and a rather unique approach, if you ask me.
Anyhow, thanks again,
Riccarda
More information about the Programming
mailing list