tags:

views:

111

answers:

1

I have a config file that contains the following line:

pre_args='$REGION','xyz',3

Using perl from within a csh script I can evaluate environment variable $REGION like so:

set pre_args = `grep -v '^#' $config_file | grep pre_args | cut -f 2 -d = | sed 's/ //g'`
if ("$pre_args" != "") then
   set pre_args = `echo $pre_args | perl -ne 'use Env; s/(\$\w+)/$1/eeg; print'`
endif

If $REGION = SOUTH, $pre_args is now set to: SOUTH,xyz,3.

Is there a way to do this using the shell's built-in commands and not having to rely on using perl? By the way, the choice to use csh is not mine, so please no replies criticizing that. Thanks.

+1  A: 

You could use eval, which parses and executes its arguments in the context of the current shell - something like this may work:

eval set pre_args = $pre_args

Edit: good point re. the quotes - thinking about it I'm surprised it evaluated the variable at all. Combining eval with an extra sed (to escape the quotes) seems to work here, but it's ugly:

set pre_args = grep -v '^#' config | grep pre_args | cut -f 2 -d = | sed 's/ //g' | sed "s/'/\\'/g"
set REGION = SOUTH
eval set pre_args = $pre_args
echo $pre_args

gives me 'SOUTH','xyz',3

SimonJ
If I do that twice I'm halfway there, however, I loose the single quotes: SOUTH,xyz,3
richard
whatever works :-) thanks for your help
richard

related questions