Re: Variable variable name



Chris F.A. Johnson wrote:
On 2006-09-12, Samik R. wrote:
Sorry for the cryptic subject, but I am trying to do the following (line numbers first) in a shell script:

15 type=AD
16 file=100000_10000_AD.csv
17 eval $type_10K=( `cat $file` )

I get the following error message:
+ type=AD
+ file=100000_10000_AD.csv
combinetab.sh: line 17: syntax error near unexpected token `('
combinetab.sh: line 17: `eval $type_10K=( `cat $file` )'

Isn't eval supposed to replace $type with AD and then put the contents of $file in an array names AD_10K?

The first problem is that you are using a variable named
'type_10K', not 'type'. You need to enclose the variable name in
braces to separate it from the following character: ${type}_10K.

The second is that you don't want the command substitution
performed on the first pass; escape the backticks to prevent that.

Third, quote the command to prevent filename expansion on the first
pass, and quote the $file variable (with escaped quotes) to prevent
expansion on the second pass.

eval "${type}_XX=( \`cat \"$file\"\` )"

In bash and ksh93 you can dispense with 'cat':

eval "${type}_XX=( \`< \"$file\"\` )"


Fourth, consider using the positional parameters to make your
script more portable:

set -f
set -- `cat "$file"`

Thanks for your reply, Chris. I am using bash, so the 3rd comment is applicable. Can you elucidate some more (or point to a reference) for the fourth comment? I haven't used any of them earlier.

Regards,
-Samik
.



Relevant Pages

  • Re: Variable variable name
    ... numbers first) in a shell script: ... I get the following error message: ... of $file in an array names AD_10K? ... quote the command to prevent filename expansion on the first ...
    (comp.unix.shell)
  • Re: Variable variable name
    ... numbers first) in a shell script: ... I get the following error message: ... quote the command to prevent filename expansion on the first ...
    (comp.unix.shell)
  • Re: Select Current Record for Report
    ... OK Thanks Chris. ... That took care of the error message. ... >in the Query design grid Access puts the quotes around any ... >>user selects an employee from this combo box, ...
    (microsoft.public.access.forms)
  • Re: java jdk14 install - cannot find file
    ... playing tricks, I was careful to ... do the KDE Find Files ... error message to the end of the find ... > It probably is and should be a Bourne shell script; ...
    (freebsd-questions)
  • Re: unclosed token expat
    ... "unenclosed token" from a C compiler, ... But if it's from the shell script you should be asking ... expect an error message about an "unclosed token". ... Just do a Google search for "unclosed token at line 1". ...
    (comp.lang.c)