Reputation: 75
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.
Upvotes: 0
Views: 1696
Reputation: 21306
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
Upvotes: 2