You are not logged in.

#1 2010-02-20 09:41:05

zfsd
Member
Registered: 2010-01-13
Posts: 15

Bash -- how to put a variable in the array's name? [solved]

I've tried different combinations, of which all returns "command not found"

array$j[$n]=$f
array${j}[$n]=$f
"array${j}"[$n]=$f
etc...


What's the right syntax?
Thanks!:)

Last edited by zfsd (2010-02-20 23:02:54)

Offline

#2 2010-02-20 09:49:10

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash -- how to put a variable in the array's name? [solved]

printf -v

--> arr[1]=4
--> q=r
--> printf -v ar$q[2] 5
--> echo ${arr[1]}
4
--> echo ${arr[2]}
5

Offline

#3 2010-02-20 12:39:55

zfsd
Member
Registered: 2010-01-13
Posts: 15

Re: Bash -- how to put a variable in the array's name? [solved]

Thanks, but the printf doesn't do anything for me.

Basically, I'm writing a script for concatenating a bunch of files.

I need to be able to set a variable in the name of the array for the script to work.

This is how I want the arrays to look:

./main.sh: line 84: job1[1]=1: command not found
./main.sh: line 68: job2[1]=2: command not found
./main.sh: line 68: job3[1]=3: command not found
./main.sh: line 68: job4[1]=4: command not found
./main.sh: line 68: job5[1]=5: command not found
./main.sh: line 68: job6[1]=6: command not found
./main.sh: line 68: job7[1]=7: command not found
./main.sh: line 68: job8[1]=8: command not found
./main.sh: line 68: job9[1]=9: command not found
./main.sh: line 84: job9[2]=10: command not found
./main.sh: line 68: job10[1]=11: command not found
./main.sh: line 68: job11[1]=12: command not found
./main.sh: line 84: job11[2]=13: command not found
./main.sh: line 68: job12[1]=14: command not found
./main.sh: line 84: job12[2]=15: command not found
./main.sh: line 84: job12[3]=16: command not found
./main.sh: line 84: job12[4]=17: command not found
./main.sh: line 84: job12[5]=18: command not found
./main.sh: line 84: job12[6]=19: command not found
./main.sh: line 84: job12[7]=20: command not found
./main.sh: line 84: job12[8]=21: command not found

But I'm using the wrong syntax..
Why isn't the syntax job$j[$f]=$n not working?

printf -v job$j[$f] $n is not working..

Offline

#4 2010-02-20 13:02:15

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash -- how to put a variable in the array's name? [solved]

What about eval?

--> eval job$j[$f]=9
--> eval echo \${job$j[$f]}
9

Offline

#5 2010-02-20 14:08:19

zfsd
Member
Registered: 2010-01-13
Posts: 15

Re: Bash -- how to put a variable in the array's name? [solved]

FANTASTIC!

It works perfectly now! smile

So the eval concatenates the arguments and make it more readable?

Job 12
#####

    14
    15
    16
    17
    18
    19
    20
    21

Job 13
#####

    22
    23

Job 14
#####

    24
    25
    26
    27
    28

Job 15
#####

    29
    30
    31

Job 16
#####

    32

Thanks again -- you saved my day smile

Offline

#6 2010-02-20 15:47:30

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Bash -- how to put a variable in the array's name? [solved]

zfsd wrote:

So the eval concatenates the arguments and make it more readable?

More or less. From the FreeBSD sh man page: Concatenate all the arguments with spaces.  Then re-parse and execute the command.

And the bash man page: The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval.

Happy coding. big_smile


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#7 2010-02-20 23:02:33

zfsd
Member
Registered: 2010-01-13
Posts: 15

Re: Bash -- how to put a variable in the array's name? [solved]

Indeed smile

Offline

Board footer

Powered by FluxBB