Hi,
i am struggling with a chunk of code.
for ((i=1; i<=3; i++));do
one-$i ="/tmp/one.$RANDOM"
done
How to execute the above code.
In both sides(LHS and RHS), i am executing commands.
it is not allowing to execute.
can you please give the idea.
It should be very appreciable, if i get an example.
Thanks.
what are you exactly trying here ?
Please help us with some explanation.
I like to get the output as
one-1="/tmp/one.22345"
one-2="/tmp/one.22346"
I mean to say like, I want to intialize two variables, one-1 one-2 with respective values.
frans
May 12, 2011, 5:15am
4
one-$i ="/tmp/one.$RANDOM"
Won't work
use eval
no space around '='
eval "one-$i=/tmp/one.$RANDOM"
sh-3.00# for ((i=1; i<=3; i++));do echo $(eval "one-$i=one.$RANDOM"); done
sh: one-1=one.13352: command not found
sh: one-2=one.14261: command not found
sh: one-3=one.15170: command not found
sh-3.00#
it is saying like command not found..
but it is not command
$ for ((i=1; i<=3; i++));do eval "one_$i=one.$RANDOM" ; done
$ echo $one_1
one.21586
$ echo $one_2
one.22755
$ echo $one_3
one.17778
This worked for me. can you try this.
---------- Post updated at 04:05 PM ---------- Previous update was at 04:02 PM ----------
I just changed the '-' to '_', it is not interpreting the latter as a command. Not sure why.
I think bash variables shouldn't contain special chars other than '_'.
Because it neither worked with '.'
frans
May 12, 2011, 7:54am
7
Right. I should have seen that !
for ((i=1; i<=3; i++));do eval "one-$i=one.$RANDOM" ; done
"-" requirement is there.
So, any other solution to execute this commad
frans
May 12, 2011, 8:18am
9
'-' can not be in a variable name. Please, tell us what you want to do with it, maybe there's a turnaround.