Re: Re: Stupid shell script question about "read" [SOLVED]
Kevin,
Looking for a solution to *my* problem (strange networking problem with debian
testing), I saw your post and thought I'd respond.
> # set some variables to nightmarish values for testing purposes
> d='"ab\""q"' # literal value is "ab\""q"
> e='$d' # literal value is $d
> f="'ba'r'" # literal value is 'ba'r'
>
> # here's the meat of the code
> result="`echo "$d $e $f" | sed "s/'/\'\\\\\\\'\'/g" | \
> ( read -r a b c; echo "a='$a' ; b='$b' ; c='$c'" )`"
> eval "$result"
>
> # test that $a $b $c have the right values
> echo "$a $b $c"
Another, more sane way to do what you want (I think):
set `echo "$d $e $f" | sed "s/'/\'\\\\\\\'\'/g"`
a=$1; b=$2; c=$3
which will work if your data have no embedded spaces. Otherwise, pick a
character you know is not in the data (+, for example) and:
OIFS=$IFS; IFS="+"
set `echo "$d+$e+$f" | sed "s/'/\'\\\\\\\'\'/g"`
a=$1; b=$2; c=$3
IFS=$OIFS
I've been writing shell scripts for many years, and I *still* trip over this
now and again, curse a time or three, then remember that some things are done
in sub-shells or sub-processes, and finally do the IFS and set trick. :)
Neal
Reply to: