Dynamic Variable Name

Hi,

I'm trying to create a dynamic variable name, and assign it the value
of yet another variable value. Here is my code:


for name in ${fnames[ [at] ]}; do
count=`expr $count + 1`
ext=`echo $name | cut -f2 -d '.'`
newname=$yest.$ext ($yest is yesterdays date)
file$count=$newname
done


I get this:

../test[21]: file1 071218.CZ1: not found
../test[21]: file2 071218.CZ2: not found
../test[21]: file3 071218.CZ3: not found

Any help?

Thanks!
art [ Mi, 19 Dezember 2007 22:09 ] [ ID #1890002 ]

Re: Dynamic Variable Name

On 2007-12-19, art [at] unsu.com <art [at] unsu.com> wrote:
>
> Hi,
>
> I'm trying to create a dynamic variable name, and assign it the value
> of yet another variable value. Here is my code:
>
>
> for name in ${fnames[ [at] ]}; do
> count=`expr $count + 1`
> ext=`echo $name | cut -f2 -d '.'`
> newname=$yest.$ext ($yest is yesterdays date)
> file$count=$newname
> done
>
>
> I get this:
>
> ./test[21]: file1 071218.CZ1: not found

did you try

eval file$count=$newname
?
Claudio [ Mi, 19 Dezember 2007 23:50 ] [ ID #1890004 ]

Re: Dynamic Variable Name

On 12/19/2007 3:09 PM, art [at] unsu.com wrote:
> Hi,
>
> I'm trying to create a dynamic variable name, and assign it the value
> of yet another variable value. Here is my code:
>
>
> for name in ${fnames[ [at] ]}; do
> count=`expr $count + 1`
> ext=`echo $name | cut -f2 -d '.'`
> newname=$yest.$ext ($yest is yesterdays date)
> file$count=$newname
> done

You could use "eval", but an array would be more useful in this context:

file[$count]=$newname

There's better ways to write the rest of your script too. Try this:

for name in ${fnames[ [at] ]}; do
count=$(( count + 1 ))
ext="${name#*.}"
newname="$yest.$ext"
file[$count]="$newname"
done

Regards,

Ed.

>
> I get this:
>
> ./test[21]: file1 071218.CZ1: not found
> ./test[21]: file2 071218.CZ2: not found
> ./test[21]: file3 071218.CZ3: not found
>
> Any help?
>
> Thanks!
Ed Morton [ Do, 20 Dezember 2007 00:59 ] [ ID #1891046 ]

Re: Dynamic Variable Name

On Dec 19, 5:59 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
> On 12/19/2007 3:09 PM, a... [at] unsu.com wrote:
>
> > Hi,
>
> > I'm trying to create adynamicvariablename, and assign it the value
> > of yet anothervariablevalue. Here is my code:
>
> > for name in ${fnames[ [at] ]}; do
> > count=`expr $count + 1`
> > ext=`echo $name | cut -f2 -d '.'`
> > newname=$yest.$ext ($yest is yesterdays date)
> > file$count=$newname
> > done
>
> You could use "eval", but an array would be more useful in this context:
>
> file[$count]=$newname
>
> There's better ways to write the rest of your script too. Try this:
>
> for name in ${fnames[ [at] ]}; do
> count=$(( count + 1 ))
> ext="${name#*.}"
> newname="$yest.$ext"
> file[$count]="$newname"
> done
>
> Regards,
>
> Ed.
>
>
>
>
>
> > I get this:
>
> > ./test[21]: file1 071218.CZ1: not found
> > ./test[21]: file2 071218.CZ2: not found
> > ./test[21]: file3 071218.CZ3: not found
>
> > Any help?
>
> > Thanks!- Hide quoted text -
>
> - Show quoted text -


Thanks for the tips. An array is a good idea. But, I will need to
refer to these variables in an FTP command where each 'file' (the
variable has the filename) will need to be uploaded to a different
directory.

So, I do not want to rely on something like this:

cd sys\$client:\[download.sales_cons\]
put ${file[1]}
cd sys\$client:\[download.sales_cons0\]
put ${file[2]}
cd sys\$client:\[download.sales_cons1\]
put ${file[3]}
cd sys\$client:\[download.sales_cons2\]
put ${file[4]}

There must be an easier way to do that, rather than hard coding the
subscript.. I'm trying to use as much generic code as possible. So,
variables holding the filenames and such is a good start.

Thanks, and looking forward to more wisdom......
amerar [ Do, 20 Dezember 2007 07:25 ] [ ID #1891058 ]

Re: Dynamic Variable Name

On Wed, 19 Dec 2007 22:50:45 +0000 (UTC), Claudio wrote:
> On 2007-12-19, art [at] unsu.com <art [at] unsu.com> wrote:
>>
>> Hi,
>>
>> I'm trying to create a dynamic variable name, and assign it the value
>> of yet another variable value. Here is my code:
>>
>>
>> for name in ${fnames[ [at] ]}; do
>> count=`expr $count + 1`
>> ext=`echo $name | cut -f2 -d '.'`
>> newname=$yest.$ext ($yest is yesterdays date)
>> file$count=$newname
>> done
>>
>>
>> I get this:
>>
>> ./test[21]: file1 071218.CZ1: not found
>
> did you try
>
> eval file$count=$newname

eval "file$count=\$newname"

--
Stephane>
Stephane CHAZELAS [ Do, 20 Dezember 2007 08:08 ] [ ID #1891060 ]
Linux » comp.unix.shell » Dynamic Variable Name

Vorheriges Thema: shell substitution
Nächstes Thema: if true ???