Re: Variable variable name
- From: "Samik R." <samik@xxxxxxxxxxxxx>
- Date: Tue, 12 Sep 2006 12:23:38 -0600
Chris F.A. Johnson wrote:
On 2006-09-12, Samik R. wrote: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.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"`
Regards,
-Samik
.
- Follow-Ups:
- Re: Variable variable name
- From: Chris F.A. Johnson
- Re: Variable variable name
- References:
- Variable variable name
- From: Samik R.
- Re: Variable variable name
- From: Chris F.A. Johnson
- Variable variable name
- Prev by Date: Re: Variable variable name
- Next by Date: Re: Variable variable name
- Previous by thread: Re: Variable variable name
- Next by thread: Re: Variable variable name
- Index(es):
Relevant Pages
|