[prog] Shell question...

Riccarda Cassini riccarda.cassini at gmx.de
Sat Dec 11 04:28:13 EST 2004


On Fri, Dec 10, 2004 at 06:16:21PM +1100, John Clarke wrote:
> 
>     eval `echo bla blah blurb | while read FOO BAR BAZ; do echo FOO=$FOO; echo BAR=$BAR; echo BAZ=$BAZ; done `

Nice idea, that eval :-)

>     #!/bin/sh
>     # read_vars.sh
>     while test $# -ne 0
>     do  
>         read -d ' ' VALUE
>         echo "$1=\"$VALUE\""
>         shift
>     done

even nicer, 'cos this read_vars.sh can be used as a drop-in replacement
for the original read.  Well almost.  Just did a

  perl -i -pe 's/\|\s*read/| \$MYREAD/ && do {s/`/\\`/g; s/(.*)/eval `$1`/}'
  
on the sources (with $MYREAD pointing to where read_vars.sh resides),
and suddenly everything appears to work - except for some other
OS-specific things I'll have to take care of anyway.  (To be honest,
I first checked whether I do have backup copies of the scripts...
I'm not the ubergeek I'd like to be. Not yet ;-) 

Execution speed isn't too much of an issue here, so the eval is just a
bit ugly conceptually, but it's absolutely fine otherwise.  (I guess,
I might run into quoting issues in case there are quotes in $VALUE, but
this hasn't happened, so far.[*])

Neat. Thanks a lot!!

Just out of curiosity, does anyone have an idea why all this isn't
needed on HP-UX?  What surprises me most is that it doesn't seem to
depend on the shell (ksh), but rather on the OS...

Riccarda


[*] Couldn't resist playing around.  To pass through a single literal
doublequote embedded in one of the values to be assigned, it seems I
need to armour it with 9 backslashes:

  eval `echo bl\\\\\\\\\"a | ./read_vars.sh FOO`
  echo $FOO

  $ ./test.sh
  bl"a

;-))



More information about the Programming mailing list