Dynamic Variable Name

Dynamic Variable Name

am 19.12.2007 22:09:17 von art

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[@]}; 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=20071218.CZ1: not found
../test[21]: file2=20071218.CZ2: not found
../test[21]: file3=20071218.CZ3: not found

Any help?

Thanks!

Re: Dynamic Variable Name

am 19.12.2007 23:50:45 von Claudio

On 2007-12-19, art@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[@]}; 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=20071218.CZ1: not found

did you try

eval file$count=$newname
?

Re: Dynamic Variable Name

am 20.12.2007 00:59:12 von Ed Morton

On 12/19/2007 3:09 PM, art@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[@]}; 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[@]}; do
count=$(( count + 1 ))
ext="${name#*.}"
newname="$yest.$ext"
file[$count]="$newname"
done

Regards,

Ed.

>
> I get this:
>
> ./test[21]: file1=20071218.CZ1: not found
> ./test[21]: file2=20071218.CZ2: not found
> ./test[21]: file3=20071218.CZ3: not found
>
> Any help?
>
> Thanks!

Re: Dynamic Variable Name

am 20.12.2007 07:25:06 von amerar

On Dec 19, 5:59 pm, Ed Morton wrote:
> On 12/19/2007 3:09 PM, a...@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[@]}; 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[@]}; do
> count=$(( count + 1 ))
> ext="${name#*.}"
> newname="$yest.$ext"
> file[$count]="$newname"
> done
>
> Regards,
>
> Ed.
>
>
>
>
>
> > I get this:
>
> > ./test[21]: file1=20071218.CZ1: not found
> > ./test[21]: file2=20071218.CZ2: not found
> > ./test[21]: file3=20071218.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......

Re: Dynamic Variable Name

am 20.12.2007 08:08:53 von Stephane CHAZELAS

On Wed, 19 Dec 2007 22:50:45 +0000 (UTC), Claudio wrote:
> On 2007-12-19, art@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[@]}; 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=20071218.CZ1: not found
>
> did you try
>
> eval file$count=$newname

eval "file$count=\$newname"

--
Stephane>