[prog] Shell question...
Riccarda Cassini
riccarda.cassini at gmx.de
Fri Dec 10 08:13:58 EST 2004
Hi everyone,
I've got a bunch of ksh scripts that were nicely doing their job under
HP-UX, for ages. Now, I'm supposed to add some more functionality,
and, while I'm at it, port them to Linux. Easy, I thought - but at the
moment I'm stumbling over constructs of the form
echo bla blah blurb | read FOO BAR BAZ
which generally don't seem to work under Linux. Unfortunately, they're
being used all over the place...
When I run the following little test script
#!/usr/bin/ksh
echo bla blah blurb | read FOO BAR BAZ
echo $FOO
echo $BAR
echo $BAZ
under HP-UX, I get
$ ./test.ksh
bla
blah
blurb
However, under Linux, all variables are just empty, so the echos print
nothing... (BTW, this equally works with /bin/sh on HP, but not with
/bin/bash on Linux.)
To be honest, I don't really understand why this works on HP, but I can
assure you it does :-)
The Linux behaviour seems to make more sense to me, 'cos - as I
eventually figured - the 'read' after the pipe is apparently being run
in a subprocess, so the variables just go away when that process
terminates.
At least that's my current theory... Can't wait to hear yours ;-)
So, can anybody enlighten me how to get this working, or how
alternative similar constructs would look like that do work under
Linux?
I know that for a single variable I could use backticks
FOO=`echo blurb`
but I'm not sure how to extend this approach to multiple assignments in
one go... (I know this example is somewhat silly, but in real life,
the 'echo' is some complex sequence of grep/awk/sed or some such, which
usually returns a list of values). Any ideas?
Actually, I feel kind of tempted to rewrite the whole stuff in perl.
OTOH, if this particular issue is the only justification, I'm afraid it
won't quite suffice to convince my client to give me any bucks for
reinventing the wheel...
Thanks,
Riccarda
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
query...
More information about the Programming
mailing list